Again, now on the line you have if Time == 0 then you are comparing an object to a number. That won’t give you the desired results. Remember you need to check the Value property of “Time”.
if Time.Value == 0 then
You really shouldn’t have to do this though since by the time that line gets read you already know for a fact the value is zero. Everything after that While loop will run if their jailtime is zero.
To do countdowns, never rely on wait. Waits do not actually wait that exact amount of time, and your countdown will therefore be off. There are quite a few resources you can search for if you want further reading about that.
The proper way to count down is by checking the time passed, so that we know exactly how much time is left.
Here’s a quick example:
-- Written on mobile, sorry for no tabs (also untested code)
local CountdownStart = 5 -- Countdown from 5
local CountdownEnd = 0 -- Countdown to 0
local StartTick = tick()
local CountdownStepper; CountdownStepper = game:GetService("RunService").Heartbeat:Connect(function()
local TimeLeft = CountdownStart-(tick()-StartTick) -- Start minus time elapsed gives accurate time left results
print(TimeLeft)
if TimeLeft<= CountdownEnd then
warn("Countdown complete")
-- Here is where you'd handle what happens after the countdown is done, like spawning the player
CountdownStepper:Disconnect()
end
end)
So I put this together, and it did not work, also no output errors. Now I am unfirmiliar with a lot of the stuff used in this code, so I have no clue how this would really work or if its supposed to function with my code? But using the steps provided, I made this (which as stated does not work) I don’t know if I mentioned this either, or if this even provides anything to the case, but this script is cloned and undisabled once its put as the child of the value.
local CountdownStart = 5
local CountdownEnd = 0
local StartTick = tick()
local CountdownStepper; CountdownStepper = game:GetService("RunService").Heartbeat:Connect(function()
local TimeLeft = CountdownStart-(tick()-StartTick)
print(TimeLeft)
if TimeLeft<= CountdownEnd then
warn("Countdown complete")
local player = script.Parent.Parent.Parent
player.stats.Arrested = false
player:LoadCharacter()
player.Team = game.Teams.Neutral
CountdownStepper:Disconnect()
end
end)
You need to attempt to understand the code you are actually writing instead of taking the replies from here and plugging them in aimlessly. He was showing you a good way to handle a countdown in a way that is more precise.
What does your explorer look like? where is this script located? Can you post the entire script? Think about the problem you are trying to solve logically, like a recipe.
-- These functions are merely an example. You can put your own code in here depending on where you have things located.
local function PlayerIsInJail(Player) -- Is the player on the prisoner team?
if Player.Team.Name == "Prisoner" then
return true
end
end
local function GetPlayerJailTime(Player) -- What is the players jailtime?
return Player.Stats.JailTime.Value
end
-- When a players character loads in you want to do your checks.
-- This can be done using a players CharacterAdded event. Once that is done you could do something like this:
if PlayerIsInJail(Player) then -- Is the player on the prisoners team when they spawned in?
while GetPlayerJailTime(Player) > 0 do -- Is the players jailtime above 0? If so then countdown till it is 0.
wait(1)
Player.Stats.JailTime.Value = Player.Stats.JailTime.Value - 1
end
-- Anything out here will run after their jailtime has reached zero.
-- This is where you would want to release the player from jail. Lets set their team and then respawn them.
Player.Team = game:GetService("Teams").Civilians
Player:LoadCharacter()
end
The above code is an example of how you could handle it. You have a function to get the players current jail time and a function to check if they are on the prisoners team.
When a player spawns in…
We check if they are on the prisoners team.
If they are a prisoner and their jail time is greater than 0 we countdown
If they are a prisoner and their jailtime is less than or equal to 0 we skip the countdown, set their team and respawn their character.
You said the script is cloned to the value, doing it that way seems sort of clunky. The way mentioned above could be as simple as putting the Script in StarterCharacterScripts. Meaning it will only run when the character loads in and destroyed when the character respawns.
Ahh, I apologize and now understand what you mean. Thank you for providing the code above, there is a little error with finding the player on the “If PlayerIsInJail(Player) then” as it errors Player because it does not know what it is, how would I be able to fix this?
You just plug in the Player as the parameter. Getting this depends on where the script is though. If you put it in StarterCharacterScripts then it would be as simple as:
local Players = game:GetService("Players")
local Player = Players:GetPlayerFromCharacter(script.Parent)
If you are still doing the cloning thing then i would need more info on how you have the datamodel setup.
Well for my game, I have one big MainScript, so this is just pretty much in a server script located in the ServerScriptStorage,I will not be cloning it if this is all I need to do.
Then the code i posted would want to be handled inside of a CharacterAdded event. You would want to handle it somewhat like this. This is the same code i sent you before except the added PlayerAdded and CharacterAdded events. If you already have a characteradded event somewhere then just copy what i sent you before and paste it in there. No need to have two.
-- These functions are merely an example. You can put your own code in here depending on where you have things located.
local function PlayerIsInJail(Player) -- Is the player on the prisoner team?
if Player.Team.Name == "Prisoner" then
return true
end
end
local function GetPlayerJailTime(Player) -- What is the players jailtime?
return Player.Stats.JailTime.Value
end
-- When a players character loads in you want to do your checks.
-- This can be done using a players CharacterAdded event. Once that is done you could do something like this:
local function HandlePlayer(Player)
local function HandleCharacter(Player, Character)
if PlayerIsInJail(Player) then -- Is the player on the prisoners team when they spawned in?
while GetPlayerJailTime(Player) > 0 do -- Is the players jailtime above 0? If so then countdown till it is 0.
wait(1)
Player.Stats.JailTime.Value = Player.Stats.JailTime.Value - 1
end
-- Anything out here will run after their jailtime has reached zero.
-- This is where you would want to release the player from jail. Lets set their team and then respawn them.
Player.Team = game:GetService("Teams").Civilians
Player:LoadCharacter()
end
end
if Player.Character then -- Just in case the character is added before the below event is connected.
HandleCharacter(Player, Player.Character)
end
Player.CharacterAdded:Connect(function(Character)
HandleCharacter(Player, Character)
end)
end
for _, Player in pairs(Players:GetPlayers()) do -- Just incase a player connects before the below event is connected.
HandlePlayer(Player)
end
Players.PlayerAdded:Connect(HandlePlayer)
Okay so I put it in SCS, but it sitll errors the player at the "for _, Player in pairs(Players:GetPlayers()) do – Just incase a player connects before the below event is connected.
HandlePlayer(Player)
end
Just a heads up, if you are putting it in StarterCharacterScripts (as i said before) you won’t need the PlayerAdded or CharacterAdded events. Those are only used to get the Player and the character. If you are putting it in StarterCharacterScripts the character is the parent of that script.
Oh alright, also, I see in the script is decreases the value by 1 per second, and this may just be the way studio works, but it seems that the player loses 2 when it updates the value? Like I said, this could just be the way studio is presenting it.
It would only be subtracting two if the loop is running twice. If your code is in StarterCharacterScripts all it really should be doing is this as the code only gets ran once when the player spawns in.
local Character = script.Parent
local Player = Players:GetPlayerFromCharacter(Character)
-- These functions are merely an example. You can put your own code in here depending on where you have things located.
local function PlayerIsInJail(Player) -- Is the player on the prisoner team?
if Player.Team.Name == "Prisoner" then
return true
end
end
local function GetPlayerJailTime(Player) -- What is the players jailtime?
return Player.Stats.JailTime.Value
end
-- When a players character loads in you want to do your checks.
-- This can be done using a players CharacterAdded event. Once that is done you could do something like this:
if PlayerIsInJail(Player) then -- Is the player on the prisoners team when they spawned in?
while GetPlayerJailTime(Player) > 0 do -- Is the players jailtime above 0? If so then countdown till it is 0.
wait(1)
Player.Stats.JailTime.Value = Player.Stats.JailTime.Value - 1
end
-- Anything out here will run after their jailtime has reached zero.
-- This is where you would want to release the player from jail. Lets set their team and then respawn them.
Player.Team = game:GetService("Teams").Civilians
Player:LoadCharacter()
end
So I just did what you said and removed the Player function and Character function, and now it goes down by 1, so it could have possibly been that, Thank you though.