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

How to make a SellBonus well you sell something in a shop?

Hello,

So I am trying to make a sell area but I made a SellBonus value so if you put it at 2 you should get 2x money but it is not working. The error is "Error trying to use a local value "multi" (nil value)"

script.Parent.Touched:connect(function(Hit)
	local Player = game.Players:FindFirstChild(Hit.Parent.Name)
	if Player then
		local leaderstats = Player:FindFirstChild("leaderstats")
		if leaderstats then
			local Cash = leaderstats:FindFirstChild("Cash")
			local Backpack = leaderstats:FindFirstChild("Bag")
			local multi = leaderstats.Parent:FindFirstChild("PlayerStats").SellBonus
				if Backpack.Value <= 0 then
				else
				if Backpack.Value > 25 then
					
					Backpack.Value = 25
				else
				end
				Cash.Value = Cash.Value + multi * Backpack.Value
				Backpack.Value = 0
				script.Disabled = true
				wait(0.1)
				script.Disabled = false
				end
			end
		end
end)

Anybody know why? Also I made a mistake I did not put value but well I sell it gives nothing...

3 comments

0

waitforchild is your best friend.

0

For line 16, you need Cash.Value = Cash.Value + multi.Value * Backpack.Value. I think the reason being is because you are just getting a int value on line 08 and not the value of the int value

0

waitforchild in this case shouldn't matter because everything should be loaded in by the time that the part is touched by a player, but it might be smart to add a check if there is a humanoid value inside player

1 answer

The first thing that I noticed is that the variable you set for multi is the actual object in the player, that being said, you need to put multi.Value when you are doing asthmatics with that variable. The next thing that I noticed is that you're checking if the player has leaderstats. This is unnecessary because if you have a script that makes it then there will never be a time that the player doesn't have his stats. I would remove this check all together. Another thing is that FindFirstChild is pretty useless unless you're doing an if statement where you need an object to continue. I would suggest replacing all of those with WaitForChild. This would prevent nil values in scripts that run away and help out in many other situations. The script below should work for you assuming you have all of the variables defined correctly:

script.Parent.Touched:connect(function(Hit)
    local Player = game.Players:FindFirstChild(Hit.Parent.Name)
    if Player then
            local Cash = leaderstats:WaitForChild("Cash")
            local Backpack = leaderstats:WaitForChild("Bag")
            local multi = leaderstats.Parent:WaitForChild("PlayerStats").SellBonus
                if Backpack.Value <= 0 then
                else
                if Backpack.Value > 25 then

                    Backpack.Value = 25
                else
                end
                Cash.Value = Cash.Value + multi.Value * Backpack.Value
                Backpack.Value = 0
                script.Disabled = true
                wait(0.1)
                script.Disabled = false
                end
	end
end)

I hope I helped you out, and if you have any questions feel free to leave a comment.

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