Temp Ban on Death Roblox

Basically, I have been trying to make a code that will ban you if you die (personal project). I have seen some code on it, and have attempted to modify it. However, one way or another, I run into errors. Help would be much appreciated!


function BanPlayer(userid,timeinseconds,reason)
	if game.Players:GetPlayerByUserId(userid) then	
				
		timeleft = timeinseconds
		
		 hours = math.floor(timeleft/3600)

		 hs = timeleft % 3600

		 mins = math.floor(hs/60)

		 secsleft = hs%60

		game.Players:GetPlayerByUserId(userid):Kick(reason.. " in: "..hours.." hours, "..mins.." mins, and "..math.floor(secsleft).. " seconds.")
		
		local g,b = pcall(function()
			bans:SetAsync("plr_"..tostring(userid),{os.time(),reason,timeinseconds})
		end)
		if g then
			print("succesfully banned player.")
		else
			warn("error setting ban datastore: "..b)
			bans:SetAsync("plr_"..tostring(userid),{os.time(),reason,timeinseconds}) -- attempt agian.
		end
	else
		local g,b = pcall(function()
			bans:SetAsync("plr_"..tostring(userid),{os.time(),reason,timeinseconds})
		end)
		if g then
			print("succesfully banned player.")
		else
			warn("error setting ban datastore: "..b)
			bans:SetAsync("plr_"..tostring(userid),{os.time(),reason,timeinseconds}) -- attempt agian.
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	if bans:GetAsync("plr_"..plr.UserId,{os.time(),reason,timeinseconds}) then
		plr:Kick(reason.. " in: "..hours.." hours, "..mins.." mins, and "..math.floor(secsleft).. " seconds.")
	end
	plr.CharacterAdded:Connect(function(char)
		char:WaitForChild("Humanoid").Died:Connect(function()
			BanPlayer(plr.UserId,3600,"You have been banned cause you died. Expires")
		end)
	end)
end)```
1 Like

What errors are appearing in the output?

This is the error in the output.

  07:36:00.039  Stack Begin  -  Studio
  07:36:00.039  Script 'ServerScriptService.BanHandler', Line 42  -  Studio - BanHandler:42
  07:36:00.040  Stack End  -  Studio

You’re missing a line in the output, show us the line above for info about what happened