Im not sure why im getting this error below in my extra cash script.
"Workspace.$150 CASH!.Head.Money Giver:12: attempt to index local 'player' (a nil value)"
the error is for line 12
local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name)
Its the script below.
local ting = 0 function onTouched(hit) if ting == 0 then ting = 1 local humanoid = hit.Parent:findFirstChild("Humanoid") if ("Humanoid") ~= nil then local player = game.Players:GetPlayerFromCharacter(hit.Parent) local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name) if cashmoney ~= nil then cashmoney.Value = cashmoney.Value +150 wait(.1) script.Parent.Parent.Name = "1 min" wait(30) script.Parent.Parent.Name = "30 sec" wait(30) script.Parent.Parent.Name = "$150 CASH!" end end ting = 0 end end script.Parent.Touched:connect(onTouched)
The error says that player
is nil
. Why might that be?
You should look at where you defined player
:
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
The Wiki article for GetPlayerFromCharacter reads
Returns the Player associated with the given character, or nil if the [parameter] is not controlled by [any] player.
That means we get nil
if hit.Parent
isn't actually any player's Character (or for that matter a Tool or Hat in a real character)
This would happen if some random part touched your button.
The solution is to only advance when you're sure player
is not nil
:
if player then local cashmoney = game.ServerStorage.MoneyStorage:FindFirstChild(player.Name) if cashmoney ~= nil then cashmoney.Value = cashmoney.Value +150 -- etc