Cash script not changing intvalue

I have been making a game that uses values loaded in inside the player with instance.new, but when ever I try to change the value using a script I always get an error stating “Workspace.Part.ClickDetector.cashgive:6: attempt to index local ‘player’ (a nil value)”.
I have been looking across many sites to find a solution to this problem and have finally decided to write a post instead. How would I fix this? My apologies if this is a stupid question with an easy fix.

scripts used:
currency script:

game.Players.PlayerAdded:connect(function(player)
    repeat wait() until player.Character
    local stat = Instance.new("IntValue")
    stat.Name = "UScurrency"
    stat.Parent = player
end)

and cashgive script:

local player = game.Players.LocalPlayer
function GiveCash(player)
    local USCash = player.Character.UScurrency.Value;
    USCash = USCash + 5;
end;
player:WaitForChild("UScurrency").Changed:connect(function()
end)
script.Parent.MouseClick:connect(GiveCash);

When you parent the Instance “IntValue” “UsCurrency”, it’s parented to the Player.
stat.Parent = player

When you try to change the value it’s looking for “UsCurrency” in the character.
local USCash = player.Character.UScurrency.Value

It should be looking for it in the Player.

local USCash = player.UScurrency.Value

Also, I’ve noticed that the error is coming from a serverscript since it’s using a clickdetector. LocalPlayer can only be called if it’s in a local script.

If you want to get the player that clicked using a clickdetector, the parameter for the mouseclick event in the click detector should work

ClickDetector.MouseClick:Connect(function(player) 
    local USCash = player.UScurrency.Value;
    USCash = USCash + 5;
end)

thanks for the explanation and help.

1 Like