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

My sell place isn't working for my simulator, can someone help me?

Asked by
hot_spi 20
4 years ago

i was trying to make a sell button and when i tried it it says:

"Workspace.Sell.Script:7: attempt to index nil with 'Value'"

here's the script for the sell:

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then 
        local leaderstats = player:findFirstChild("leaderstats")
        local Cash = player:findFirstChild("Cash")
        local Strength = player:findFirstChild("Strength")
        if Strength.Value > 0 then
            Cash.Value = Cash.Value + Strength.Value*1
            Strength.Value = 0
        end
    end
end)

1 answer

Log in to vote
1
Answered by
incapaz 195
4 years ago

You are looking for the cash inside of the player itself as opposed to the leaderstats. You meant to look in the latter therefore Instance::FindFirstChild returns nil.

local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(part)
    local player = Players:GetPlayerFromCharacter(part.Parent)

    if player then 
        local leaderstats = player.leaderstats
        local Cash = leaderstats.Cash
        local Strength = leaderstats.Strength

        if Strength.Value > 0 then
            Cash.Value = Cash.Value + Strength.Value
            Strength.Value = 0
        end
    end
end)

And by the way you don't need Instance:FindFirstChild if you are actually able to guarantee that an instance with the provided name exists. Btw multiplying by 1 is redundant since any number multiplied by 1 results in itself

0
the error was gone but the value of the cash doesnt change when the part was touched hot_spi 20 — 4y
Ad

Answer this question