How to check if a specific player if NOT in the game

Hello! So I have a server script that manages everything and if a specific player joins the game, he will be given special benefits. The part that doesn’t work is this chunk that sees if the player’s name is not the special one, then it will kick them from the game.
Code:

if Player.Name == "TabbyCat904" then
			print("Player found.  Hello, WolfieGamerYT!")
			ServerStorage.SpeedTool:Clone().Parent = Player.Backpack
			print("tool given")
			Player.leaderstats.Coins.Value = 1000000
			Player.leaderstats.Gems.Value = 1000000
		else
			local EnemyHumanoid = Character:WaitForChild("Humanoid")
				
				if EnemyHumanoid then
					print("humanoid found")
					
					while true do
					task.wait(1)
						EnemyHumanoid:TakeDamage(5)
						EnemyHumanoid.Died:Connect(function()
						Player:Kick("Be gone, none me!")
					end)
				end
			end

If you think you know what went wrong, please let me know. Have a wonderful day!

5 Likes

You are connecting the died event every second which is not necessary, this will cause a memory leak. Additionally checking for a special user you should rather use UserId than the username.

1 Like

How would I insert that? I’ve tried going by UserId before, but it didn’t work.

By comparing the UserId of the player who you want to receive special benefits to the player who joins the game

This should work.

local PlayerService = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

PlayerService.PlayerAdded:Connect(function(Player)
     if Player.UserId == 0000000000 --[[USER ID HERE]] then
          local SpeedTool = ServerStorage.SpeedTool:Clone()
          SpeedTool.Parent = Player.Backpack
          
          local Stats = Player:WaitForChild("leaderstats")
          
          Stats.Coins.Value = math.huge
          Stats.Gems.Value = math.huge
     else
          local Character = Player.Character
          local Humanoid = Character:WaitForChild("Humanoid")

          Humanoid.Died:Connect(function()
                Player:Kick("Be gone!")
          end

          while task.wait(1) do
               Humanoid:TakeDamage(5)
          end
     end
end
2 Likes

If this worked, please mark it as a solution.

The code seems to work, but it takes health away from me, and I’m the special player.

Is the UserId the scipt is checking correct?

Yes. It’s checking for my main account.

The PlayerAdded function in @blvecoves’s code puts “Layer” as the parameter, and uses “Player” for the rest of the code.

Did you replace your script with his? In that case, correct the mistake.

I’m sorry if this isn’t relevant, but just to make it clear.

1 Like

I changed Layer to Player and it still didn’t work.

Are there any errors in the output?

Fixed that typo. Thanks for pointing it out!

2 Likes

@Kittylitterking123 I’ve made some changes to the code. Let me know if it works now. Make sure you replace the numbers with the UserID you want to have the abilities.

What exactly is and is not working?

With the code, I can’t even play the game. It kicks me like it’s supposed to other players.

I’ve made another edit to the code. Let me know again what happens.

It still didn’t work. Did I do something wrong?

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		
		repeat wait() until Character:FindFirstChild("Head") -- This is just to give the player a nametag above their head, just ignore it lol
		local newNametag = Nametag:Clone()
		newNametag.Parent = Character.Head
		newNametag.Adornee = Character.Head
		newNametag.TextLabel.Text = Player.DisplayName
		
		task.wait()
		if Player.UserId == "3081295884" then
			print("Player found.  Hello, WolfieGamerYT!")
			local SpeedTool = ServerStorage.SpeedTool:Clone()
			SpeedTool.Parent = Player.Backpack
			print("tool given")
			
			local Stats = Player:WaitForChild("leaderstats")
			
			Stats.Coins.Value = math.huge
			Stats.Gems.Value = math.huge
		else
			local Humanoid = Character:WaitForChild("Humanoid")
			
				while task.wait(1) do
					Humanoid:TakeDamage(5)
					
					if Humanoid.Health == 0 then
						break
					end
				end
			Player:Kick("Begone!")
		end
	end)
end)
1 Like

I see. You put quotes around the numbers. You weren’t supposed to do that lol.

Player.UserId is always a number, not a string. The specified ID became one when you added quotes, erroring the code.

If removing the quotes worked, please mark my original post as a solution.

If doing so doesn’t work, please let me know.

1 Like

It doesn’t kick the player whenever the humanoid.Health reaches 0.

I’ve made a quick change. Try it once more.