Error: 'Player argument must be a Player object'

I can’t seem to find the Player’s object and pass it through a RemoteEvent. The RemoteEvent fires but I still get the error. I’ve looked at other topics but none have worked.

TouchPart.Touched:Connect(function(Object)
    local Player = game:GetService("Players"):GetPlayerFromCharacter(Object.Parent)
end)

Thanks,
Skylexion

Not every part that touches is the character.

Object can be any part in the game that touches your TouchPart.

You should add some if statements that check if it is a character.

Here’s how to get the player object correctly:

TouchPart.Touched:Connect(function(obj)
    local character = obj:FindFirstAncestorWhichIsA("Model") --The touched part can even be an accessory, you need the model (character)
    if character then --If a model was found
        local player = game:GetService("Players"):GetPlayerFromCharacter(character) --Get the player from character model.
        if player then
            --Now you have the correct player object, and you can do :FireClient(player)
            print(player.Name)
        end
    end
end)

I basically first checked if the part is in a model (possible character), then if it is a player character

3 Likes

it’s more ideal to (edit: check for the humanoid) by doing local humanoid = obj.Parent:FindFirstChild("Humanoid") because there could be models that are welded to the character. using obj.Parent is a sure way to know that a bodypart has touched the part because bodyparts are always one children away from the character.

TouchPart.Touched:Connect(function(object_)
	local Humanoid = object_.Parent:FindFirstChild("Humanoid")
	
	if Humanoid then
		local Character = object_.Parent
		local Player = game:GetService("Players"):GetPlayerFromCharacter(Character)
		
		if Player then
			print("found player")
		end
	end
end)

While this work’s, it’s redundant when we can just do:

TouchPart.Touched:Connect(function(object_)
	local Character = object_.Parent
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Character)
		
	if Player then
		print("found player and this is an actual player")
	end
end)

There’s a check making sure it is a player’s character.

You also know the character is a model. There’s no difference.

The advantage of using :FindFirstAncestorWhichIsA("Model") is that it will also work for accessories, and it assures you that your outcome is a model.

Using object.Parent, you can get so many other results since .Touched does not promise you you’ll get a body part.


A humanoid check works too, but I disagree about the object.Parent part.

unless, of course, there is a welded model to the character, which i said.

And if there is a welded model to the character, object.Parent gets you the welded model or part.

Why do you think that is different, or more ideal?

Both of our codes have a check that makes sure it is a character anyways.

I do not mean to offend, I am just wondering what’s the reason behind the change