Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is the parent of UserId and why is my code erroring?

So I have been scripting a door so that when you touch it some of your data will load into the game. Well for whatever reason the UserId is returning this error: "UserId is not a valid member of Model". Here is the code:

script.Parent.Touched:Connect(function(hit)

	if hit.Parent:FindFirstChild("Humanoid") then

		local plr = hit.Parent
		local specialKey = "User_"..plr.UserId

		-- more code here but that is irrelevant.
	
	end
end)

What is even more confusing however, is the fact that in this script it works:

game.Players.PlayerAdded:Connect(function(plr)

	local specialKey = "User_"..plr.UserId

end)

2 answers

✓ Accepted answer

You were trying to get the UserId from the player's Character rather than from the Player itself.

script.Parent.Touched:Connect(function(hit)

    if hit.Parent:FindFirstChild("Humanoid") then

        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        local specialKey = "User_"..plr.UserId

        -- more code here but that is irrelevant.

    end
end)

1 comment

1

Oh! Thank you so much.

Hi Phlegethon,

The issue with your script is simple. One is addressing the Character, and one's addressing the player. The character is what appears in the server, it's your model in the game that's touchable and that has parts and others can see it. The player is in the Players, it's a Player Instance that has other information about the player. The Character is just a medium for the Player. Anyways, I won't get too into that but, your UserId is located in the player, not the character. So, that's why when you used the Players with the .PlayerAdded event, it worked because you were addressing the Player. The Character doesn't have the UserId. But, there's a simple fix for how to get the Player from the Character. Here, I'll show you how to do this using your script:

script.Parent.Touched:Connect(function(hit)

    if hit.Parent:FindFirstChild("Humanoid") then

        local character = hit.Parent
        local player = game.Players:GetPlayerFromCharacter(character); -- This gets the player from the character.
	local specialKey = "User_"..player.UserId;

        -- more code here but that is irrelevant.

    end

##Well, I hope I helped and have a nice day/night.

Thanks,

Best regards,

~~ KingLoneCat

This discussion is preserved as it was. New answers, comments, and votes are closed.