What if... The Player has the same name as a Model in workspace

Basically if someone joins your game and has the name “Model”, (unlikely but there are other names too)
what actions could you take to avoid getting the players character while also getting the object you wanted to get when scripting,
using as little amount of code as possible. I don’t want to be using:

local ignorePlayer
game.Players.PlayerAdded:Connect(function(player)
    for i, player in pairs(game.Players:GetPlayers()) do
        if player.Name == NAME_HERE then
            ignorePlayer = player
        end
    end
end

-- other code

if game.Workspace:WaitForChild("Model") ~= ignorePlayer then
    game.Workspace.Model:DOSTUFF()
end

this would take alot of time and sometimes would not work. you would also have to implement it in every script.
Is there a way to do this? I’m not really in a rush or anything, this is no problem for me. I’m just asking for future references. A what if question! :slightly_smiling_face: any feedback helps, thank you.

You could move the player model to a separated folder in Workspace to keep them sanctioned from each other, rename the players’ model, or check if it contains a humanoid

3 Likes

If you really want to tell if the model is a player, you actually can; and you can also tell specifically which player it is.

In the Humanoid, there’s a property called DisplayName, which you can use to tell if it’s a player. However, the client can change it so if you’re looking to do this as an anti exploit then it’s no good.
Screenshot 2021-12-25 at 8.52.37 pm

2 Likes

On the event that a player does have the same name as a model you can simply do

local Players = game:GetService("Players")

if not Model:FindFirstChildOfClass("Humanoid") and not Players:FindFirstChild(Model.Name) then
--Code here
end

to ignore any models that have humanoids in them and also check if the player exists on the player list.

Otherwise if you are doing stuff from a server script you can simply reference the model you wish to work with on a variable, since the server will be the first one to be there before any player even joins

--Sets model as a variable
local Model = workspace:FindFirstChild("ModelName")
--Player with the name ModelName joins
print(Model) --Will return the first model which was already in the workspace rather than the player that just joined
1 Like
game.Workspace.ChildAdded:Connect(function(Model)
    if Model:IsA("Player") then
        model.Parent = game.Players
    else
        return
    end
end)
1 Like

Or you can see if :GetPlayerFromCharacter(model) returns nil…

2 Likes

In this situation it’d just be best to have all the characters parented to a folder, checking if the instance you indexed is a player’s character is just unnecessary and you’d have to implement it for every script.

local Plrs = game:GetService("Players")
local PlrFolder = Instance.new("Folder", workspace)

local PlrCons = {}

-- for every new player, parent their character to that folder when character spawns
Plrs.PlayerAdded:Connect(function(Player)
	PlrCons[Player] = Player.CharacterAdded:Connect(function(Char)
		Char.Parent = PlrFolder 
	end)
end)

-- disconnect the characteradded connection
Plrs.PlayerRemoving:Connect(function(Player)
	if PlrCons[Player] then
		PlrCons[Player]:Disconnect()
		PlrCons[Player] = nil
	end
end)

-- for every already existing player, parent their character to that folder when character spawns
-- if character already exists, it'll be parented to the folder
for _, Player in pairs(Plrs:GetPlayers()) do
	if Player.Character then
		Player.Character.Parent = PlrFolder
	end
	
	PlrCons[Player] = Player.CharacterAdded:Connect(function(Char)
		Char.Parent = PlrFolder
	end)
end
2 Likes

Yeah i reccomend your method because i think tis the easiest

1 Like

This will never work, like at all.
Player instances are only parented to Players service, you should be using Players:GetPlayerFromCharacter() instead.

1 Like