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

why I have this error "attempt to index field'LocalPlayer' (a nil value)" ???

Asked by 4 years ago

Good morning, everyone, So I'm making a tycoon and I want when I pass on the collector, to get the money back so I made this script:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then   
        game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value + script.Parent.Parent.Screen.amount.Value
    end 
end)

And when I run the script I get the following error: "attempt to index field'LocalPlayer' (a nil value)", I looked on other forums but nothing, I don't know what to do. Help me please

0
You cant use game.Players.LocalPlayer on a server script XviperIink 428 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

This should work:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
    local Player = game.Players:GetPlayerFromCharacter(hit.Parent) --  get the player from the character
    local Cash = Player.leaderstats.Cash
        Cash.Value = Cash.Value + script.Parent.Parent.Screen.amount.Value
    end 
end)

If it helped, remember to accept! :D

Ad
Log in to vote
0
Answered by 4 years ago

Thank you Xviperlink, but now, I have this error: "Player is not a valid member of DataModel"

0
game.Players:GetPlayerFromCharacter(hit.Parent) AizakkuZ 226 — 4y
0
Thanks @AizakkuZ, I don't understand why I have "Screen is not a valid member of UnionOperation" now ?? Altair77100 6 — 4y
0
script.Parent.Parent.Screen.amount.Value is the cause as you put script.Parent.Parent try directly indexing it AizakkuZ 226 — 4y
0
Finally, it's works, thanks @AizakkuZ Altair77100 6 — 4y

Answer this question