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

Why is money not taken away from player?

local Object = "SolarPanel1"
local Price = 10000
local Color = script.Parent.Parent.Parent.Color.Value

local DB = false
script.Parent.Touched:connect(function(buyer)
	if buyer.Parent:FindFirstChild("Humanoid") and buyer.Parent:FindFirstChild("Torso") then
		if buyer.Parent.Name == script.Parent.Parent.Parent.Owner.Value then
			local BMoney = game.Players:FindFirstChild(buyer.Parent.Name).leaderstats.Money.Value
			if BMoney >= Price then
				DB = true
				BMoney = BMoney - Price
				game.Lighting:FindFirstChild(Color):FindFirstChild(Object):Clone().Parent = script.Parent.Parent.Parent.Objects
				script.Parent:Destroy()
				DB = false
			end
		end
	end
end)

So, I'm making a tycoon, and this is the script to purchase items. But for some reason, money isn't taken away from the buyer when it is activated.

Any help?

1 answer

✓ Accepted answer

Your variable BMoney is simply a variable that has the value of game.Players:FindFirstChild(buyer.Parent.Name).leaderstats.Money.Value . By changing BMoney you do not change the value of game.Players:FindFirstChild(buyer.Parent.Name).leaderstats.Money.Value ingame.

To fix that simply put the modified value of BMoneyin game.Players:FindFirstChild(buyer.Parent.Name).leaderstats.Money.Value :

local Object = "SolarPanel1"
local Price = 10000
local Color = script.Parent.Parent.Parent.Color.Value

local DB = false
script.Parent.Touched:connect(function(buyer)
    if buyer.Parent:FindFirstChild("Humanoid") and buyer.Parent:FindFirstChild("Torso") then
        if buyer.Parent.Name == script.Parent.Parent.Parent.Owner.Value then
		local playerMoney= game.Players:FindFirstChild(buyer.Parent.Name).leaderstats.Money -- easier to write
            local BMoney =playerMoney.Value
            if BMoney >= Price then
                DB = true
                BMoney = BMoney - Price
		playerMoney.Value=BMoney -- <-- change the money
                game.Lighting:FindFirstChild(Color):FindFirstChild(Object):Clone().Parent = script.Parent.Parent.Parent.Objects
                script.Parent:Destroy()
                DB = false
            end
        end
    end
end)

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