就像 cashpersecond 函数实际上并不是每秒,尽管我不太确定是什么原因造成的。
而且每次我抢劫时增量都会增加一秒,这太奇怪了。
脚本如下:
local maxCash = 3000
local cashPerSecond = 500
local timeToRob = 300
local robbing = false
local cooldown = 30
local closed = false
local defaultVaultPosition = script.Parent.VaultDoor.Position
local playersRobbing = {}
local alreadyRobbed = {}
local guis = {}
function endRobbery()
closed = true
robbing = false
wait(1)
script.Parent.Door.CanCollide = true
script.Parent.VaultDoor.Position = defaultVaultPosition
for i, gui in pairs(guis) do
gui:Destroy()
end
for p, x in pairs(playersRobbing) do
p.Character.HumanoidRootPart.CFrame = script.Parent.FailedToRob.CFrame
end
playersRobbing = {}
alreadyRobbed = {}
script.Alarm:Stop()
wait(cooldown)
closed = false
script.Parent.Door.CanCollide = false
end
function beginRobbery()
for i = 1, timeToRob do
wait(1)
local region3 = Region3.new(script.Parent.VaultArea.Position - script.Parent.VaultArea.Size/2, script.Parent.VaultArea.Position + script.Parent.VaultArea.Size/2)
local parts = workspace:FindPartsInRegion3(region3, script.Parent, math.huge)
local playersGivenCashTo = {}
for x, part in pairs(parts) do
if playersRobbing[game.Players:GetPlayerFromCharacter(part.Parent)] and playersRobbing[game.Players:GetPlayerFromCharacter(part.Parent)] < maxCash and not playersGivenCashTo[game.Players:GetPlayerFromCharacter(part.Parent)] then
playersGivenCashTo[game.Players:GetPlayerFromCharacter(part.Parent)] = true
playersRobbing[game.Players:GetPlayerFromCharacter(part.Parent)] += cashPerSecond
if not game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui:FindFirstChild("RobberyGui") then
script.RobberyGui:Clone().Parent = game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui
game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui.RobberyGui.StatsHUD.CashBagHUD.Cash.Amount.Text = "$0"
table.insert(guis, game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui.RobberyGui)
game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui.RobberyGui.StatsHUD.CashBagHUD.Cash.Visible = true
game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui.RobberyGui.StatsHUD.CashBagHUD.CashIcon.Visible = true
end
game.Players:GetPlayerFromCharacter(part.Parent).PlayerGui.RobberyGui.StatsHUD.CashBagHUD.Cash.Amount.Text= "$" .. playersRobbing[game.Players:GetPlayerFromCharacter(part.Parent)]
end
end
end
endRobbery()
end
local touchedCooldown = {}
script.Parent.Door.Touched:Connect(function(hit)
if not closed and game.Players:GetPlayerFromCharacter(hit.Parent) and not touchedCooldown[game.Players:GetPlayerFromCharacter(hit.Parent)] and not playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)] and not alreadyRobbed[game.Players:GetPlayerFromCharacter(hit.Parent)] then
script.Alarm:Resume()
touchedCooldown[game.Players:GetPlayerFromCharacter(hit.Parent)] = true
playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)] = 0
spawn(function()
wait(2)
touchedCooldown[game.Players:GetPlayerFromCharacter(hit.Parent)] = nil
end)
if not robbing then
beginRobbery()
end
robbing = true
elseif not closed and game.Players:GetPlayerFromCharacter(hit.Parent) and not touchedCooldown[game.Players:GetPlayerFromCharacter(hit.Parent)] and playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)] then
-- Remove this section
end
end)
-- Connect to the Trigger part instead of the Door part
game.Workspace.Doors.DoubleKeyDoorCriminal.Trigger.Touched:Connect(function(hit)
if not closed and game.Players:GetPlayerFromCharacter(hit.Parent) and not touchedCooldown[game.Players:GetPlayerFromCharacter(hit.Parent)] and playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)] then
-- on hit collect money
game.Players:GetPlayerFromCharacter(hit.Parent).leaderstats.Cash.Value += playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)]
playersRobbing[game.Players:GetPlayerFromCharacter(hit.Parent)] = nil
alreadyRobbed[game.Players:GetPlayerFromCharacter(hit.Parent)] = true
local stillRobbing = false
for _, x in pairs(playersRobbing) do
if x then stillRobbing = true end
end
if not stillRobbing then
endRobbery()
end
end
end)
script.Parent.OpenVault.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
wait(3)
game:GetService("TweenService"):Create(script.Parent.VaultDoor, TweenInfo.new(5), {Position = defaultVaultPosition + Vector3.new(0, script.Parent.Door.Size.Y, 0)}):Play()
end
end)
我尝试过
playersRobbing[game.Players:GetPlayerFromCharacter(part.Parent)] += cashPerSecond + wait(1)
,哪种方法有效,但它使增量变成奇怪的小数,同时还传递了最大量。如果有人可以更多地解释这个脚本那就太好了
除了缺乏使用变量之外,解决方案相当简单: 每条指令都需要一定的时间来执行。 与 WorldRoot 交互的函数是占用最多时间的指令的主要部分。
因为你有很多 :GetPlayerFromCharacter 函数,所以时间可能会偏离你的目标 1 秒。
wait() 函数已被弃用,最小量可以是 0.30 秒,因此我们可以从中得出结论,它将以 0.30 秒的分区等待。 任务调度程序的新函数 task.wait() 的最小等待时间为 0.016 秒,这使得它比 wait() 函数更准确地等待一秒。
为了确保玩家每秒收到增量的钱,您需要使用协程。截至目前,您可以使用 task.spawn() 函数使其在不同的线程上运行,从而绕过执行其他指令所需的等待时间:
...
task.spawn(function()
task.wait(1)
playersRobbing[player] += cashPerSecond
end)
-- rest of the code
...