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

Datastore problems and help on duplication and rejoining?

Asked by
fr2013 88
5 years ago
Edited 5 years ago

So I am currently creating three tabs of inventory, which are swords, potions, and magic. They are all made in Backpack. My Backpack GUI does register them each and they are also cloned in a specific folder when I buy them from the shop. So when I die, it'll save the tools and there place inside. But it duplicates the folder (2x) one with the tools and the other which is empty, while the tools duplicate (2x) inside of the folder that has tools. And also it doesn't save the tools when I leave and then rejoin the game too.

local InStudio = game:GetService("RunService"):IsStudio()

if not InStudio then
    sword = game:GetService("DataStoreService"):GetDataStore("Swords")
    potion = game:GetService("DataStoreService"):GetDataStore("Potions")
    magic = game:GetService("DataStoreService"):GetDataStore("Magic")
end

function savedata(dataname, playerid, value)
    if InStudio then return end
    dataname:SetAsync(playerid, value)
end

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(char)

        local data1 = nil
        local data2 = nil
        local data3 = nil

        data1 = sword:GetAsync(player.UserId)
        data2 = potion:GetAsync(player.UserId)
        data3 = magic:GetAsync(player.UserId)

        local sword = Instance.new("Folder")
        sword.Name = "Swords"
        sword.Parent = player:WaitForChild("Backpack")
        local potion = Instance.new("Folder")
        potion.Name = "Potions"
        potion.Parent = player:WaitForChild("Backpack")
        local magic = Instance.new("Folder")
        magic.Name = "Magic"
        magic.Parent = player:WaitForChild("Backpack")

        local sword = Instance.new("Folder")
        sword.Name = "Swords"
        sword.Parent = player:WaitForChild("StarterGear")
        local potion = Instance.new("Folder")
        potion.Name = "Potions"
        potion.Parent = player:WaitForChild("StarterGear")
        local magic = Instance.new("Folder")
        magic.Name = "Magic"
        magic.Parent = player:WaitForChild("StarterGear")

    char.Humanoid.Died:connect(function()
        char.Humanoid:UnequipTools()

        local old = player.StarterGear:GetChildren()
        for i = 1, #old do
            old[i]:Destroy()
        end

        local new = player.Backpack:GetChildren()
        for i = 1, #new do
            new[i].Parent = player.StarterGear
        end     
    end)
    end)
end)

game.Players.PlayerRemoving:connect(function(player)
    savedata(sword, player.userId, player.Backpack:WaitForChild("Swords"))
    savedata(potion, player.userId, player.Backpack:WaitForChild("Potions"))
    savedata(magic, player.userId, player.Backpack:WaitForChild("Magic"))
end)
0
You never call getAsync so you arent gonna pull any saved data. As i mentioned on your last post i believe the duplicate folder issue is to do with you calling it on character added. DinozCreates 1070 — 5y
0
So do I put like a data = sword:GetAsync(player.UserId) in player.CharacterAdded:Connect(function(char) ? fr2013 88 — 5y
0
whats data? i dont see where you've defined that anywhere DinozCreates 1070 — 5y
0
Ok I edited it fr2013 88 — 5y
View all comments (15 more)
0
use locals, and name them seperately. local data1 = ... local data2 = ... we cant save them into 1 variable, currently youre just changing what it holds 3 times DinozCreates 1070 — 5y
0
Ok so I re-edited it and tested out in my game, but it isn't copied in the Swords folder, just in the Backpack, even though it saves and doesn't duplicate fr2013 88 — 5y
0
So I create new folders in StarterGear? fr2013 88 — 5y
0
You dont need to nil them, just local data 1 = yada yada yada. Then player.sword = data1. See if that makes stuff work. DinozCreates 1070 — 5y
0
Im not a huge fan of the way you've set things up. I creates bools for any obtainable objects then true/false if owned or not. if true then on connect theyre added to player. Could do something similar with potions, just use int value to determine how many then add them. DinozCreates 1070 — 5y
0
Re-edit and I don't think it still works ;-; fr2013 88 — 5y
0
I didnt mean to literally put player.sword, make that the location of your swords folder. DinozCreates 1070 — 5y
0
So I re-edited it again and it still has the same effect fr2013 88 — 5y
0
Yeah i figured. I dont think datastores can work the way you're trying to do it. You'll probably have to save them item by item. DinozCreates 1070 — 5y
0
@DinozCreates Well I re-edited it and I solved the problem of duplicating items by creating a folder in StarterGear, now how do I save them when the player leaves and then rejoins? fr2013 88 — 5y
0
create values for each item, add them all into tables, save the values from the tables into your data stores, save the table data into the item values. DinozCreates 1070 — 5y
0
So is it like this?: savedata(sword, player.userId, player.Backpack:WaitForChild("Swords")) and savedata(sword, player.userId, player.StarterGear:WaitForChild("Swords")) and sword:GetAsync(player.UserId) or are you actually talking about making individually all my items save? Like the swords and potions? fr2013 88 — 5y
0
option #2 DinozCreates 1070 — 5y
0
Dude your function is not cool hence i wont help you LightModed 81 — 5y
0
Dude you never called GetAsync hence ur not getting what you stored LightModed 81 — 5y

Answer this question