Strange Behavior in my Jail script

Hello! I have recently found that my jail script had a bug where mass resetting would cause the player to be released early due to starting a countdown that removes a minute from the jail stat every time. If I do this based on when the player is added the same problem is created by rejoining. On top of this when I tried to test starting the loop when the player was added if the player reset the loop ends. This is very strange behavior and I don’t understand how to work around these issues. Any suggestions you may have helps.

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAdded:connect(function(char)
		wait(1.5)
		jailTime(plr,char)
	end)	
	local MinutesRemaining = plr:WaitForChild("leaderboard").jailtimer
	while wait(60) and MinutesRemaining.Value >= 0 do
		MinutesRemaining.Value -= 1
		if MinutesRemaining.Value == 0 then
			MinutesRemaining.Value = -1
			MuteUser:FireClient(plr, false)
			wait(.1)
			plr:LoadCharacter()
		end
	end
end)
1 Like

could you post the function jailtime() ?

also by mass resetting did you mean multiple players reseting all at once?

The loop isn’t in jailtime, so any errors with it shouldn’t be caused there, especially since there are no more loops running. By mass resetting I just mean spamming the reset key makes multiple loops start and I can’t think of a way to only run the loop once per player without some hacky solution like some sort of boolean stored in the player.

to fix the mass resetting problem just add every jailed player to a table and every time the script is supposed to start the jailtime loop, use a for loop to cycle through the table and check if the player in question has already had the loop play for them.

at first it will be empty but you should use the jailtime function to add jailed players to the table so if they reset it will play again but it will notice it has already started a loop for them and will not start a new loop

maybe something like this:

Jailed = {}

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAdded:connect(function(char)
		for index, player in pairs(Jailed)
			if player == plr then return end -- check if player that was added is in table jailed
		end
		wait(1.5)
		jailTime(plr,char) -- insert code into this function to add the player into Jailed preventing the loop from starting again
	end)	

	local MinutesRemaining = plr:WaitForChild("leaderboard").jailtimer
	while wait(60) and MinutesRemaining.Value >= 0 do
		MinutesRemaining.Value -= 1
		if MinutesRemaining.Value == 0 then
			MinutesRemaining.Value = -1
			MuteUser:FireClient(plr, false)
			wait(.1)
			plr:LoadCharacter()
		end
	end
end)

by creating a variable:

local PlayerCache = {}

PlayerCache[player] = true

if PlayerCache[player] then
    return
end

make sure to set that value to nil when they leave the game

also if you post the jailtime function we can get a better idea of how the whole script works.

That should fix the problem. Thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.