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

My buy script isn't working. It only gives u the tool and doesn't take money from u. Please Help!?

Scripts:

local data = game:GetService("DataStoreService"):GetDataStore("Hack3214234hg")
game.Players.PlayerAdded:connect(function(plr)
	local stats = Instance.new("Folder",plr)
	stats.Name = "leaderstats"
	
	local Money = Instance.new("NumberValue",stats)
	Money.Name = "Money"
	
	local SavedItems = data:GetAsync(plr.UserId)
	if SavedItems then
		Money.Value = SavedItems.Money or 20
	else
		Money.Value = 20
	end
end)

script.Parent.MouseButton1Click:connect(function()
	local price = script.Parent.Parent.Price
	local player = game.Players.LocalPlayer
	if game.Players.LocalPlayer.leaderstats.Money.Value >= price.Value then
		local item = script.Parent.Parent.Item
		local backpack = player.Backpack
		local starter = player.StarterGear
		local clone = game.ReplicatedStorage.Tools:FindFirstChild(item.Value):Clone()
		local clone2 = game.ReplicatedStorage.Tools:FindFirstChild(item.Value):Clone()
		clone.Parent = backpack
		clone2.Parent = starter
		game.ReplicatedStorage.Buy:FireServer()
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local Saving = {["Money"] = plr.leaderstats.Money.Value;
	}
	data:SetAsync(plr.UserId,Saving)
end)

game.ReplicatedStorage.Buy.OnServerEvent:Connect(function(player)
	player.leaderstats.Money.Value = player.leaderstats.Money.Value - player.Price.Value
end)

It doesnt save the leaderstats if i have it in a local script...

2 comments

0

Hello! can you put your scripts inside Code block?

0

uh ok

1 answer

You need to add one more thing [Look at line 5]

script.Parent.MouseButton1Click:connect(function()
    local price = script.Parent.Parent.Price
    local player = game.Players.LocalPlayer
    if game.Players.LocalPlayer.leaderstats.Money.Value >= price.Value then
       game.Players.LocalPlayer.leaderstats.Money.Value - price.Value -- This is what you need to add
        local item = script.Parent.Parent.Item
        local backpack = player.Backpack
        local starter = player.StarterGear
        local clone = game.ReplicatedStorage.Tools:FindFirstChild(item.Value):Clone()
        local clone2 = game.ReplicatedStorage.Tools:FindFirstChild(item.Value):Clone()
        clone.Parent = backpack
        clone2.Parent = starter
        game.ReplicatedStorage.Buy:FireServer()
    end
end)

I hope that helps you. Good luck.

6 comments

1

Uh the problem is that it isnt saving when im doing that

1

Do you mean when you rejoin the game doesn't save leaderstats value?

1

Or the tool?

1

dude you need to explain your answers. If you didnt he will never learn lua

1

The leaderstats

1

Please make another question. I can't reply again here.

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