Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

1 comment

0

You cant use game.Players.LocalPlayer on a server script

2 answers

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

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

4 comments

0

game.Players:GetPlayerFromCharacter(hit.Parent)

0

Thanks @AizakkuZ, I don't understand why I have "Screen is not a valid member of UnionOperation" now ??

0

script.Parent.Parent.Screen.amount.Value is the cause as you put script.Parent.Parent try directly indexing it

0

Finally, it's works, thanks @AizakkuZ

This discussion is preserved as it was. New answers, comments, and votes are closed.