I have a script which makes your time go up by 1 every second. My game is a sword fighting game and I want a safezone which makes you invincible, but people could use this to farm time so I want to make your time stay.
Im using a touched event to detect when you go in a safe zone.
local players = game:GetService("Players")
player = players.PlayerAdded:Wait()
print(player.Name)
local coro = coroutine.create(function()
while task.wait(1) do
player.leaderstats.Time.Value -= 1
end
end)
function whentouchedOn(hit)
coroutine.yield(coro)
if not hit.Parent:FindFirstChild("Humanoid") then return end
if hit.Parent:FindFirstChild("ForceField") then return end
local forcefield = Instance.new("ForceField", hit.Parent)
forcefield.Visible = false
end
script.Parent.Touched:Connect(whentouchedOn)
function whentouchedOff(hit)
coroutine.resume(coro)
if not hit.Parent:FindFirstChild("Humanoid") then return end
if hit.Parent:FindFirstChild("ForceField") then
hit.Parent.ForceField:Destroy()
end
end
script.Parent.TouchEnded:Connect(whentouchedOff)
The problem is your time continues to go up and no errors
This function returns a table, so you could look for a HumanoidRootPart, check its parent’s name to get the player’s name, and put the player in a “paused” list. The function you use to give players time could check whether the player is paused or not.
Example:
local Players = game:GetService("Players")
local Safezone = workspace.mySafeZone
while task.wait(1) do
local safePlayers = {}
local parts = Safezone:GetPartBoundsInBox()
for _, part in pairs(parts) do
if part.Name ~= "HumanoidRootPart" then return end
if not part.Parent:IsA("Model") then return end
if not Players:FindFirstChild(part.Parent.Name) then return end
table.insert(part.Parent.Name)
end
for _, plr in pairs(Players:GetChildren()) do
if table.find(safePlayers, plr.Name) then return end
-- give player +1 second time
end
end