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:
01 | script.Parent.Touched:Connect( function (hit) |
02 |
03 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
04 |
05 | local plr = hit.Parent |
06 | local specialKey = "User_" ..plr.UserId |
07 |
08 | -- more code here but that is irrelevant. |
09 |
10 | end |
11 | end ) |
What is even more confusing however, is the fact that in this script it works:
1 | game.Players.PlayerAdded:Connect( function (plr) |
2 |
3 | local specialKey = "User_" ..plr.UserId |
4 |
5 | end ) |
You were trying to get the UserId from the player's Character rather than from the Player itself.
01 | script.Parent.Touched:Connect( function (hit) |
02 |
03 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
04 |
05 | local plr = game.Players:GetPlayerFromCharacter(hit.Parent) |
06 | local specialKey = "User_" ..plr.UserId |
07 |
08 | -- more code here but that is irrelevant. |
09 |
10 | end |
11 | 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:01 | script.Parent.Touched:Connect( function (hit) |
02 |
03 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
04 |
05 | local character = hit.Parent |
06 | local player = game.Players:GetPlayerFromCharacter(character); -- This gets the player from the character. |
07 | local specialKey = "User_" ..player.UserId; |
08 |
09 | -- more code here but that is irrelevant. |
10 |
11 | end |
Thanks,
Best regards,
~~ KingLoneCat