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

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

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

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

1 comment

0

the error was gone but the value of the cash doesnt change when the part was touched

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