How do I make a script that destroys the model when the player name is not Shrank

So how do I make a script that destroys the model I tried a script

for i, v in pairs(game.Players:GetPlayers()) do
	if v.Name == "shrank" then
		print("Shrank Whitelist Done!")
	else
		print("You don't own the product!")
		script.Parent.Parent.Parent.ShranksSCI:Destroy()
	end
end

But it won’t work and btw this is in a server script

You might need to check if there’s any player in the game. I tested in my studio and when it doesn’t have wait() it’s doesn’t run because no one is in the game, I used wait(5) and it’s run because I was in the game.

wait(5)

for i, v in pairs(game.Players:GetPlayers()) do
	if v.Name == "shrank" then
		print("Shrank Whitelist Done!")
	else
		print("You don't own the product!")
		script.Parent.Parent.Parent.ShranksSCI:Destroy()
	end
end

like this?

A different way from what @EPIC_Friendly said, you can do this, it will also insure the player doesn’t see the model in the first 5 seconds.

game.Players.PlayerAdded:Connect(function(plr)  -- checks when player is added
	if plr.UserId == USERIDHERE then  -- I would recommend using UserId so it cannot change
		print("Shrank Whitelist Done!")
	else
		print("You don't own the product!")
		script.Parent.Parent.Parent.ShranksSCI:Destroy()
	end
end)

Hope this helps!

Why are you using a for loop? Is the intended purpose of this script so that if the only person in the server is shrank, the model stays, but if there is anyone else, the model goes away? If so, use the Players.PlayerAdded event.

Be wary of the differences between usernames and nicknames. “Name” will be their username, whereas “DisplayName” will be their nickname. Capitalization can also vary, so instead of doing it like that, you can do this:

game:GetService("Players").PlayerAdded:Connect(function(player)
  if player.Name:lower() == "shrank" then
    -- Whitelist
  else
   --
  end
end)
2 Likes

Yea this looks better than mine. I recommend you (@StarryShrank) to use this. It’s run the script when the player joins the game.

1 Like

Thank you, This script How do I make a script that destroys the model when the player name is not Shrank - #5 by enumBenjamin and this script How do I make a script that destroys the model when the player name is not Shrank - #4 by Oonzii works!