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
6 years ago
Edited 6 years ago
1script.Parent.Touched:Connect(function(hit)
2    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
3    if player.leaderstats.Points.Value > 10 then
4        player:LoadCharacter()
5        player.PlayerGui.Enabled = true
6    end
7end)

1 answer

Log in to vote
1
Answered by 6 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.

1script.Parent.Touched:Connect(function(hit)
2    if hit.Parent:FindFirstChildWhichIsA('Humanoid') then
3        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
4        if player then
5            player:LoadCharacter()
6            -- rest of code
7        end
8    end
9end)

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 — 6y
Ad

Answer this question