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

why does GetPlayerFromCharacter returns nil?

Asked by
CjayPlyz 643 Moderation Voter
5 years ago
Edited 5 years ago
script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player.leaderstats.Points.Value > 10 then
        player:LoadCharacter()
        player.PlayerGui.Enabled = true
    end
end)

1 answer

Log in to vote
1
Answered by 5 years ago

In your function, you have it set to where when anything touches it, it will get passed as the player and then the script will attempt to edit the Points of the object that touched it.

To fix this, you should add an if statement which checks if the object that touched it has a humanoid, if it does then you can declare the player variable.

Checking if an object has a humanoid ensures that it is a player who is touching it and so it won't return nil.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChildWhichIsA('Humanoid') then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            player:LoadCharacter()
            -- rest of code
        end
    end
end)

However, I'm not sure what you're trying to do by 'player.PlayerGui.Enabled' because that isn't a property of PlayerGui and you cannot access PlayerGui using a ServerScript, so you should read up on RemoteFunctions here and use that instead.

0
thanks CjayPlyz 643 — 5y
Ad

Answer this question