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

Can you use 2 Data store in a game?

Asked by 5 years ago

Hello, so I am learning how to save player data and I am making a Clothes Shop. Now I have to save the clothes that players bought. I can save those but when I try to save the cash, it is unable to be saved. Can someone help me with this code?

local ds = game:GetService("DataStoreService"):GetDataStore("PlayerDataStrings09")
local ds2 = game:GetService("DataStoreService"):GetDataStore("PlayerCash07")

game.Players.PlayerAdded:connect(function(player)
    local key = "user:"..player.userId
    local folder = Instance.new("Folder",player)
    folder.Name = "leaderstats"
    local cash = Instance.new("IntValue",folder)
    cash.Name = "Cash"
    cash.Value = 1000
    local folder2 = Instance.new("Folder",player)
    folder2.Name = "Clothes"
    local currentoutfit = Instance.new("StringValue",folder2)
    currentoutfit.Name = "CurrentOutfit"
    local shirt = Instance.new("StringValue",currentoutfit)
    shirt.Name = "Shirt"
    local pants = Instance.new("StringValue",currentoutfit)
    pants.Name = "Pants"

    player.CharacterAdded:connect(function(character)
        if shirt.Value ~= "" then
            character:WaitForChild("Shirt").ShirtTemplate = shirt.Value
            character:WaitForChild("Pants").PantsTemplate = pants.Value
        end
    end)
    local save = ds:GetAsync(key)
    if save then
        cash.Value = save[1]
        for i,v in pairs(save) do
            if game.ReplicatedStorage.Clothes:FindFirstChild(v) then
                local realClothes = game.ReplicatedStorage.Clothes:FindFirstChild(v):Clone()
                realClothes.Parent = folder2
            end
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local key = "user:"..player.userId
    local cash = player.Cash
    ds:SetAsync(key,cash.Value)
end)

game.ReplicatedStorage.BuyShirt.OnServerEvent:Connect(function(player,shirt,pants,cName)
    local key = "user:"..player.userId
    player.Character:WaitForChild("Shirt").ShirtTemplate = shirt
    player.Character:WaitForChild("Pants").PantsTemplate = pants
    player:WaitForChild("Clothes").CurrentOutfit.Shirt.Value = shirt
    player:WaitForChild("Clothes").CurrentOutfit.Pants.Value = pants
    local clothes = Instance.new("BoolValue",player.Clothes)
    clothes.Name = cName
    local newShirt =Instance.new("StringValue",clothes)
    newShirt.Name = "Shirt"
    newShirt.Value = shirt
    local newPants =Instance.new("StringValue",clothes)
    newPants.Name = "Pants"
    newPants.Value = pants
    local clothes = {}
    for i,v in pairs(player.Clothes:GetChildren()) do
        if v.Name ~= "CurrentOutfit" then
            table.insert(clothes,v.Name)
            ds:SetAsync(key,clothes)            
        end
    end
end)

2 answers

Log in to vote
0
Answered by
herrtt 387 Moderation Voter
5 years ago

Yes, you can use 2 data stores in one game. Read more on the wiki. http://robloxdev.com/api-reference/class/DataStoreService http://robloxdev.com/code-sample/DataStore-Budget

Ad
Log in to vote
0
Answered by 5 years ago

Now I have 2 Data Stores. But when I buy clothes and left the game and rejoin back, my cash is still 1000. Can anyone help me with this script?

local ds = game:GetService("DataStoreService"):GetDataStore("0PlayerDataStrings09")
local ds2 = game:GetService("DataStoreService"):GetDataStore("0PlayerCash07")

game.Players.PlayerAdded:connect(function(player)
    local key = "user:"..player.userId
    local key2 = "user:"..player.userId.."cash"
    local folder = Instance.new("Folder",player)
    folder.Name = "leaderstats"
    local cash = Instance.new("IntValue",folder)
    cash.Name = "Cash"
    cash.Value = 1000
    local folder2 = Instance.new("Folder",player)
    folder2.Name = "Clothes"
    local currentoutfit = Instance.new("StringValue",folder2)
    currentoutfit.Name = "CurrentOutfit"
    local shirt = Instance.new("StringValue",currentoutfit)
    shirt.Name = "Shirt"
    local pants = Instance.new("StringValue",currentoutfit)
    pants.Name = "Pants"

    player.CharacterAdded:connect(function(character)
        if shirt.Value ~= "" then
            character:WaitForChild("Shirt").ShirtTemplate = shirt.Value
            character:WaitForChild("Pants").PantsTemplate = pants.Value
        end
    end)
    local save = ds:GetAsync(key)
    local save2 = ds2:GetAsync(key2)
    if save then
        for i,v in pairs(save) do
            if game.ReplicatedStorage.Clothes:FindFirstChild(v) then
                local realClothes = game.ReplicatedStorage.Clothes:FindFirstChild(v):Clone()
                realClothes.Parent = folder2
            end
        end
    end
    if save2 then
        cash.Value = save2[1]
    else
        ds:SetAsync(key2,cash.Value)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local key2 = "user:"..player.userId.."cash"
    local cash = player.leaderstats.Cash    
    ds:SetAsync(key2,cash.Value)

end)

game.ReplicatedStorage.BuyShirt.OnServerEvent:Connect(function(player,shirt,pants,cName)
    local key = "user:"..player.userId
    player.Character:WaitForChild("Shirt").ShirtTemplate = shirt
    player.Character:WaitForChild("Pants").PantsTemplate = pants
    player:WaitForChild("Clothes").CurrentOutfit.Shirt.Value = shirt
    player:WaitForChild("Clothes").CurrentOutfit.Pants.Value = pants
    local clothes = Instance.new("BoolValue",player.Clothes)
    clothes.Name = cName
    local newShirt =Instance.new("StringValue",clothes)
    newShirt.Name = "Shirt"
    newShirt.Value = shirt
    local newPants =Instance.new("StringValue",clothes)
    newPants.Name = "Pants"
    newPants.Value = pants
    local clothes = {}
    for i,v in pairs(player.Clothes:GetChildren()) do
        if v.Name ~= "CurrentOutfit" then
            table.insert(clothes,v.Name)
            ds:SetAsync(key,clothes)            
        end
    end
end)
0
you need to save the money herrtt 387 — 5y
0
That's what I did there XxxLloyd061302xxX 5 — 5y

Answer this question