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

How can I fix this script?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

So I'm working on an obby stage system, and this is my script for the bricks:

script.Parent.Touched(function(hit)
    p = hit.Parent:GetPlayerFromCharacter(hit.Parent)
    if p then
        p.leaderstats.Stage = p.leaderstats.Stage + 1
    end
end)


Something is wrong with it, but I'm not sure what. Can anyone help me with the GetPlayerFromCharacter function? I don't really understand it.

3 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

GetPlayerFromCharacter is a method of the Players service -- you don't use it on a model, you give a model to the players service:

p = game.Players:GetPlayerFromCharacter( hit.Parent )
Ad
Log in to vote
2
Answered by 9 years ago

You're using GetPlayerFromCharacter wrong. You actually need to use this function in Players. Example:

game.Players:GetPlayerFromCharacter(Character)

You don't use this on the character because this function's argument IS the character. Here is the correct code:

script.Parent.Touched(function(hit)
    p = game.Players:GetPlayerFromCharacter(hit.Parent)
    if p then
        p.leaderstats.Stage = p.leaderstats.Stage + 1
    end
end)

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

Your problem is that GetPlayerFromCharacter is a method of the Players service, so you must use it like this:

game.Players:GetPlayerFromCharacter()

This takes one argument (that is, the thing in the parentheses), a Character model. It then attempts to find the Player that's operating the given Character. If it's successful in doing this, it returns the Player. Otherwise it returns nil.

If your Left Leg made the event fire, that means Left Leg is hit. Therefore hit.Parent must be your Character. Knowing this we can write

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

and it will return your Player.

But the Part might have been touched by something else that's Parent isn't a Character, so we need to make sure that GetPlayerFromCharacter didn't return nil.

if player then
    --do stuff
end

Answer this question