Player argument must be a player object

I’ll make this as quick as I can, this code isn’t working when it should:

script.Parent.Touched:Connect(function(hit)
	local player = hit.Parent
	game.ReplicatedStorage.SpawnNewStair:FireClient(game.Players:GetPlayerFromCharacter(player))
end)

Yes, the touched event fires.

2 Likes

It is possible that hit.Parent is another object and not a part.

1 Like

I’m guessing it returns nil, try this code out:

local PS = game:GetService("Players")

local part: BasePart = script.Parent

part.Touched:Connect(function(hit: BasePart)
	local char = hit:FindFirstAncestorWhichIsA("Model")
	local player = char and PS:GetPlayerFromCharacter(char)
	
	if player then
		game.ReplicatedStorage.SpawnNewStair:FireClient(player)
	end
end)
1 Like
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid") then
		local player = hit.Parent
		game.ReplicatedStorage.SpawnNewStair:FireClient(game.Players:GetPlayerFromCharacter(player))
	end
end)
1 Like

Turns out that I needed to change it to:

if hit.Parent:FindFirstChild("Humanoid") then
			game.ReplicatedStorage.SpawnNewStair:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
		else
			game.ReplicatedStorage.SpawnNewStair:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent.Parent))
		end

I forgot about accessories :man_facepalming:

Sorry if I wasted anyone’s time

2 Likes
script.Parent.Touched:Connect(function(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if typeof(player) == 'Instance' then
		game:GetService("ReplicatedStorage").SpawnNewStair:FireClient(player)
	end
end)
1 Like

You can just use

:FindFirstAncestorWhichIsA("Model")

to check for a model ancestor instead of checking if the touch part’s parent is a character or it’s parent’s parent is a character (and so on and so forth).