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

What can I do to stop an error of 'player'(a nill value)"?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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)
0
Im not sure why it spaced down like that, its not like that in the script. brooksdart 5 — 9y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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
0
im very new to scripting, im trying to do as much as I can with out getting exact answers but in this case im not exactly sure. Do I replace line 12 with this line? Thnaks so much for your help. brooksdart 5 — 9y
0
Wait I think I get it. The script I have is asking to get player from hit.Parent and the what you added is to state that if hit.Parent is player then... ext am I close? brooksdart 5 — 9y
0
Just like you check that `cashmoney ~= nil`, you need to check that `player` isn't `nil`. In the snippet you posted, that means inserting an `if player ~= nil` before line 12 and a matching `end` after line 23. BlueTaslem 18071 — 9y
0
ok thanks so much brooksdart 5 — 9y
Ad

Answer this question