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.
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 )
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)
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