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)
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)
Hi Phlegethon,
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
Thanks,
Best regards,
~~ KingLoneCat