A problem with a Ban Script

Hey, again! I’ve got a problem and noticed nothing working with the scripts, there was no error in the console however there is an underline circle on the:

if success then

I have no other idea to fix this script, I’ve tried debugging it however nothing happens.

local DataStoreService = game:GetService("DataStoreService")
local banDataStore = DataStoreService:GetDataStore("banDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		
		local userID = Player.UserId
		
		local success, errormessage = pcall(function()
			banDataStore:SetAsync(userID, true)
			
			if Character:FindFirstChild("Humanoid").Health == "0" then
				if success then
					print("someone got banned")
					
					Player:Kick("you died, and got banned. well rip")
				end
			end
		end)
	end)
end)

Any problem between the script?

You’re creating your if condition within the pcall itself. success and errormessage don’t exist inside its scope.

Two problems:

  • You never ended the pcall.
  • The Character:FindFirstChild("Humanoid").Health will probably never equal zero, this is because the player’s health can’t go down to zero as soon as they spawn. Use Humanoid.Died instead.

Ah, I’ll try to fix it. Is there any more problems within it?

well, you’re not checking if the player is banned when they join but I assume you’ve not got to that part yet.

Nevermind, I expect to close this topic as I’ve gotten a new idea, thank you for those who assisted me.

local DataStoreService = game:GetService("DataStoreService")
local banDataStore = DataStoreService:GetDataStore("banDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		
		local userID = Player.UserId
		
		local success, errormessage = pcall(function()
			banDataStore:SetAsync(userID, true)
			
			if Character:FindFirstChild("Humanoid").Health == 0 then
				if success then
					print("someone got banned")
					
					Player:Kick("you died, and got banned. well rip")
				end
			end
		end)
	end)
end)

The problem was you put “0” instead of 0.

Try checking if it succeded after the call

local DataStoreService = game:GetService("DataStoreService")
local banDataStore = DataStoreService:GetDataStore("banDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		
		local userID = Player.UserId
		
		local success, errormessage = pcall(function()
			banDataStore:SetAsync(userID, true)
        end)
        
        if success then
			if Character:FindFirstChild("Humanoid").Health == 0 then -- make sure it 0 not "0" as "0" is a string and 0 is a number
				print("someone got banned")
					
			    Player:Kick("you died, and got banned. well rip")
			end
		end
	end)
end)
1 Like

Alright, I’ll try to do all of this.

Still don’t work. I tried to do the things it was discussed in the post.

maybe change the character added to

local Character = player.Character or player.CharacterAdded:Wait()