Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

player.userID is not a valid member of part?

Asked by 9 years ago

Hello!

I just created a script to promote a person in my group using the http service, however it doesn't seem to work.

function promoteuser(..,..,..) 
    print "This function is classified and works fine."
end

function promote(player)
    print "1"
    promouser((1240173),(8236279), player.userId) 
    print "fin"
end

script.Parent.Touched:connect(promote)

The error is on line 7.

userId is not a valid member of Part

Can someone help?

Thanks!

0
Have you define UserID? Usually that will happen if you have not perfectly define where UserID is. Anciteify 70 — 9y
0
Or player it could either be them. Anciteify 70 — 9y

2 answers

Log in to vote
1
Answered by
yumtaste 476 Moderation Voter
9 years ago

The Touched event returns the part that touched the part that the event is focusing on. For example, part1 is the part we're focusing on (touching it will trigger the Touched event). Say a player's character's left leg touches it. In your case, the variable "player" is the left leg, not the player. So, I suggest you change the "player" variable to something that makes more sense with what we're dealing with here, like "part." After you do that, you use the GetPlayerFromCharacter() method to get the player from his/her character. Your script would then look like this:

script.Parent.Touched:connect(function(part) --this is what yours does, I just like to write my events a different way
if part.Parent:IsA("Model") then --this checks if the part's parent is a Model, since the player's character is a model
local player = game.Players:GetPlayerFromCharacter(part.Parent) --this line will attempt to get a player from a character. if there's not a valid character, this will be nil, which is why we have the next line
if player then
promouser((1240173),(8236279), player.userId)
end
end
end)

If this answer was helpful, please consider upvoting it and marking it as "accepted." Thanks!

0
I'd recommend adding "if not part.Parent then return end" to prevent erroring on a self-removing projectile (which would cause part.Parent:IsA("Model") to error because part.Parent would be nil) chess123mate 5873 — 9y
0
Ty! :D WelpNathan 307 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

The error explains what's going on. You're attempting to access "userId" from "player", but it's telling you that the variable "player" refers to a Part - and Parts don't have userIds.

Indeed, you say "script.Parent.Touched:connect(promote)", and a Touched event's argument is what touched (in this case) script.Parent. Thus, you need to convert the part to the player:

function promote(part)
    if part.Parent == nil then return end --ignore projectiles
    local player = game.Players:PlayerFromCharacter(part.Parent)
    if player == nil then return end --not a player
    --rest of promote here
end

Also, make sure that nothing bugs if 'promote' gets triggered by the same player numerous times in a row (ex if multiple limbs touch 'script.Parent' at the same time).

Answer this question