Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How can I save a player's gear that they received? (Read Desc)

Asked by 10 years ago

I have an in-game shop where players can spend points on fun gears. I tested this by purchasing a gear for fifty points. I received the gear, but when I left the game and returned.. the gear had gone, along with the points to buy it.

Help?

2
Read the Data Persistence/Data Store article in Wikipedia. OniiCh_n 410 — 10y

1 answer

Log in to vote
0
Answered by 10 years ago

This will save the gear to data store as a encoded table. This script can go anywhere in the game as long as it is not disabled. MAKE SURE: The gear is in the player`s startergear.

local ds=game:GetService("DataStoreService"):GetDataStore("Gear")  --Get`s the DataSoreService
function savegear(player)
    local gear={ } --Sets up a table for use later
    for i,v in pairs(player.StarterGear:GetChildren())do --Gets the gear from the players starter gear
        table.insert(gear,#gear+1,v) --Inserts gear into table
    end
    return gear --Returns the table filled with gear
end

game.Players.PlayerRemoving:connect(function(plr)
    savedgear=savegear(plr) --Saves gear
    ds:SetAsync(plr.userId.."_Gear",game.HttpService:JSONEncode(savedgear)) --Sets the table as a string
end)

game.Players.PlayerAdded:connect(function(plr)
    loadedgear=game.HttpService:JSONDecode(ds:GetAsync(plr.userId.."_Gear")) --Decodes the table
    if loadedgear~=nil then --Makes sure there is a value
        for i,v in pairs(loadedgear)do
            v.Parent=plr.StarterGear --Loads gear in player`s startergear
        end
    end
end)

Observe this code, edit it to save points instead of gear. CAUTION: This might work no guarantees, feel free to edit the code if needed.

Ad

Answer this question