Temporary ban won't save

Hello there.

I made a temp ban script on death, but when I rejoin I’m still able to join.

Here is my ban function:

function BanPlayer(userid,timeinseconds,reason)
	if game.Players:GetPlayerByUserId(userid) then
		local timeleft = timeinseconds

		local hours = math.floor(timeleft/3600)

		local hs = timeleft % 3600

		local mins = math.floor(hs/60)

		local 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

Here is the full script:

local bans = game:GetService("DataStoreService"):GetDataStore("ebanned12")

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)

function BanPlayer(userid,timeinseconds,reason)
	if game.Players:GetPlayerByUserId(userid) then
		local timeleft = timeinseconds

		local hours = math.floor(timeleft/3600)

		local hs = timeleft % 3600

		local mins = math.floor(hs/60)

		local 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.PlayerRemoving:Connect(function(plr)
	if workspace[plr.Name].Humanoid.Health <= 40 then
		BanPlayer(plr.UserId,7200,"You have been banned cause you left while having 40 or less HP. Expires")
	else
		BanPlayer(plr.UserId,60,"You have been banned to prevent combat loggers with 41 HP and above. Expires")
	end
end)

This is you, check on an alt account. The owner of a game cant be banned.

Yes they can.

Unless it is a admin script like “adonis”, the owner can be banned.

1 Like

Don’t even know what you’re doing here. GetAsync only has one parameter.

Where is reason, hours, mins, and secsleft defined?

2 Likes

I actually got it to work with these apparent issues.

And it is defined right here:

function BanPlayer(userid,timeinseconds,reason)
	if game.Players:GetPlayerByUserId(userid) then
		local timeleft = timeinseconds

		local hours = math.floor(timeleft/3600)

		local hs = timeleft % 3600

		local mins = math.floor(hs/60)

		local secsleft = hs%60