Hello, I am trying to add more time to a timer by the player touching a part and giving the timer +10 seconds, but I can’t try figure out how to make it work.
Timer
local player = game.Players.LocalPlayer
-- Set the time limit to 30 seconds
local timeLimit = 30
while timeLimit > 0 do
-- Wait 1 second
wait(1)
-- Decrement the time limit by 1 second
timeLimit = timeLimit - 1
-- Print the remaining time to the console
print(string.format("%d seconds remaining", timeLimit))
end
if timeLimit <= 0 then
-- Kill the player
player.Character:BreakJoints()
-- Print a message to the console
print("Time's up! Player has been killed.")
end
Also if this helps the script goes into StarterCharacterScripts and the script is local.
You may not want to run this on the client. Exploiters could delete your script and just live forever.
Instead, connect Players.PlayerAdded and run that.
Example:
local Players = game:GetService("Players") -- Get the players service
Players.PlayerAdded:Connect(function(player) -- Connect the PlayerAdded function so this occurs with every player.
-- Initialize timeLimit
local timeLimit = 30
while timeLimit > 0 do
-- Wait 1 second
task.wait(1)
-- Decrement the time limit by 1 second
timeLimit = timeLimit - 1
-- Print the remaining time to the console
print(string.format("%d seconds remaining", timeLimit))
end
if timeLimit <= 0 then
-- Kill the player
player.Character.Humanoid.Health = 0
-- Print a message to the console
print("Time's up! Player has been killed.")
end
end)
-- This is based on your original code, not the code that would actually fix your problem
Now, for your actual problem, you may want to have a NumberValue in the player character named “Time”. We reference that NumberValue as a variable in the while loop, setting timeLimit to that NumberValue’s value, upon an update. Instead of updating timeLimit, we update the NumberValue’s value property. When our timeLimit variable is equal to 0, we kill the player.
An example showing how this would be done:
Players.PlayerAdded:Connect(function(player) -- Connect the PlayerAdded function so this occurs with every player.
local timeValue = Instance.new("NumberValue") -- Create the number value...
timeValue.Name = "Time" -- .. name it "Time"...
timeValue.Value = 30 -- .. set its value to 30...
timeValue.Parent = player -- .. and parent it to the player.
-- Initialize timeLimit
local timeLimit = timeValue.Value
while timeLimit > 0 do
-- Wait 1 second
task.wait(1)
-- Decrement the time limit by 1 second
timeValue.Value -= 1
timeLimit = timeValue.Value
-- Print the remaining time to the console
print(string.format("%d seconds remaining", timeLimit))
end
if timeLimit <= 0 then
-- Kill the player
player.Character.Humanoid.Health = 0
-- Print a message to the console
print("Time's up! Player has been killed.")
end
end)
Then, in your part:
local Players = game:GetService("Players"
script.Parent.Touched:Connec(function(otherPart) -- Connect the touched function when a player touches the part
local char = otherPart.Parent -- Get the part's parent (aka the player's character); we don't get the model from .Touched
local player = Players:GetPlayerFromCharacter(char) -- Get the player who touched the part
if player then -- Make sure our player exists (this function could be triggered if any random part touches this part)
local time = player:FindFirstChild("Time") -- Get our "Time" value
if time then -- Make sure it exists. This isn't really necessary, but there is the off chance this is triggered before the "Time" value is created
time.Value += 10
end
end
end)
It works but how would you make it to where the timer goes down when they die then gain more time, and how would I go about to make the time cap at 30 seconds?
So, after our if time then statement, we need to put another if then after that, if time > 0 then. Then, we also need to, after our time.Value is set the first time, we set time.Value to math.clamp(time.Value, 0, 30).
So that would look something like this:
local Players = game:GetService("Players"
script.Parent.Touched:Connec(function(otherPart) -- Connect the touched function when a player touches the part
local char = otherPart.Parent -- Get the part's parent (aka the player's character); we don't get the model from .Touched
local player = Players:GetPlayerFromCharacter(char) -- Get the player who touched the part
if player then -- Make sure our player exists (this function could be triggered if any random part touches this part)
local time = player:FindFirstChild("Time") -- Get our "Time" value
if time then -- Make sure it exists. This isn't really necessary, but there is the off chance this is triggered before the "Time" value is created
if time.Value > 0 then -- Make sure we aren't dead
time.Value += 10
time.Value = math.clamp(time.Value, 0, 30) -- Clamp the time's current value between 0 and 30: we can't go below 0 or above 30
end
end
end
end)
In that case you would need to take the existing loop and put it inside Player.CharacterAdded instead of Players.PlayerAdded.
It’s a simple fix, really.
Here is what your code should look like once this is finished:
Players.PlayerAdded:Connect(function(player) -- Connect the PlayerAdded function so this occurs with every player.
local timeValue = Instance.new("NumberValue") -- Create the number value...
timeValue.Name = "Time" -- .. name it "Time"...
timeValue.Value = 30 -- .. set its value to 30...
timeValue.Parent = player -- .. and parent it to the player.
player.CharacterAdded:Connect(function(char) -- Run this every time the player respawns
timeValue.Value = 30 -- Reset the value so we don't die as soon as we respawn
-- Initialize timeLimit
local timeLimit = timeValue.Value
while timeLimit > 0 do
-- Wait 1 second
task.wait(1)
-- Decrement the time limit by 1 second
timeValue.Value -= 1
timeLimit = timeValue.Value
-- Print the remaining time to the console
print(string.format("%d seconds remaining", timeLimit))
end
if timeLimit <= 0 then
-- Kill the player
player.Character.Humanoid.Health = 0
-- Print a message to the console
print("Time's up! Player has been killed.")
end
end)
end)