Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How could I save gear purchased with in-game currency?

Asked by
yodafu 16
2 months ago

Hi guys, I've built a script for an ingame shop GUI that allows players to buy gear with "points" which is a leaderstats currency and is saved using datastores. However, i've noticed a major issue with this script which is that when a player leaves the game or dies, the gear they purchased is removed from their inventory and they'd have to buy it again. Here is my script so far...

player = game.Players.LocalPlayer

points = player.leaderstats.Points

price = script.Parent.Cost.Value

tool = game.Lighting.Tools.Speedcoil







function buy()

if points.Value >= price then

points.Value = points.Value - price

local a = tool:clone()

a.Parent = player.Backpack

local b = tool:clone()

b.Parent = player.StarterGear

end

end



script.Parent.MouseButton1Down:Connect(buy)

Please note that this is inside a localscript.

Any help is appreciated!

1 answer

Log in to vote
0
Answered by 2 months ago

You can save gears that they have bought in-game.

All you would have to do is create a script for a datastore and do something like this:

local DataStore = game:GetService("DataStoreService"):GetDataStore("GearData")

local GameTools = game:GetService("ReplicatedStorage"):WaitForChild("Tools")

game.Players.PlayerAdded:Connect(function(Player)
    local Data = DataStore:GetAsync(Player.UserId)
    if Data then
        for i,v in pairs(Data) do
            local Tool = GameTools:FindFirstChild(v)
            if Tool then
                local ClonedTool = Tool:Clone()
                ClonedTool.Parent = Player:WaitForChild("StarterGear")
                if not Player:WaitForChild("Backpack"):FindFirstChild(ClonedTool.Name) then
                    local ClonedTool2 = Tool:Clone()
                    ClonedTool2.Parent = Player:WaitForChild("Backpack")
                end
            end
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local Backpack = Player:WaitForChild("Backpack")
    local Tools = {}
    for i,v in pairs(Backpack:GetChildren()) do
        table.insert(Tools,v.Name)
    end
    DataStore:SetAsync(Player.UserId,Tools)
end)

Not sure if it will work like that I just wrote that code on a text editor not roblox studio but it would work something like that for a datastore to save tools that the player has

0
unaccepting cus just spoon feeding full code incapaxx 3107 — 2mo
Ad

Answer this question