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

How do I get this DataStore Inventory save working properly?

Asked by 6 years ago

Hello, I know enough to know what I don't know. I have a DataStore that saves leaderstats just fine, a Shop that adds items to the players Backpack and StarterPack. I'm having difficulty with getting the weapons to save after they player has died. I don't see any tutorials online about how to do this, and "look at the wiki" just confuses me. I know the most common answer I see when other people ask on Youtube or the Forums is, "add a boolValue to the player that gets enabled when a weapon is purchased, and have the datastore check that."

Okay, here's what I've done, for the purpose of trying to keep an "M9" with the player after they purchase the item from the Shop and are killed/respawn.

1. I added a Bool Value under Players labeled "HasM9"

2. I added a line to the purchase script for the M9 in my shop that sets the Players Bool Value to true:


-- Shop purchase code above, Adding item to Backpack/Starter Gear below: tool:Clone().Parent = plr.Backpack tool:Clone().Parent = plr.StarterGear plr.leaderstats.Points.Value = plr.leaderstats.Points.Value - amount game.Players.HasM9.Value = true -- I have no problems with the Shop, it adds/denies purchases just fine.

3. I have a Data Store that saves values that looks like this:

game.Players.PlayerAdded:connect(function(player)
        local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

    player:WaitForChild("leaderstats")
    wait(1)
    local stats = player:FindFirstChild("leaderstats"):GetChildren()
    for i = 1, #stats do            
    stats[i].Value = datastore:GetAsync(stats[i].Name)
    print("stat number "..i.." has been found")

    stats[i].Changed:connect(function() 
local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
for i =  1, #statstorage do
    datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
    print("saved data number "..i)

end
print("Stats successfully saved")   
end)
    end

end)

Again, no problem saving the leaderstats values, so I tried adding the "HasM9" value...and I'm pretty sure how I went about it is all wrong. I have my items stored in ReplicatedStorage and a folder under ServerStorage called "ToolStorage".

4. My edited DataStore code:


game.Players.PlayerAdded:connect(function(player) local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats") player:WaitForChild("leaderstats") wait(1) local stats = player:FindFirstChild("leaderstats"):GetChildren() for i = 1, #stats do stats[i].Value = datastore:GetAsync(stats[i].Name) print("stat number "..i.." has been found") -- Added "HasM9" lines below -- local HasM9 = player:GetAsync(player.userId) or false if HasM9 then game.ServerStorage.ToolStorage.M9:Clone().Parent = player.Backpack -- Added "HasM9" lines above -- stats[i].Changed:connect(function() local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats") local statstorage = player:FindFirstChild("leaderstats"):GetChildren() for i = 1, #statstorage do datastore:SetAsync(statstorage[i].Name, statstorage[i].Value) print("saved data number "..i) end print("Stats successfully saved") end) end end -- extra End added on account of the M9 code. end)

Am I remotely close to getting this right? I could really appreciate the help here. Thank you!

1
just 1 tip, never ever ever ever ever ever ever ever ever use the Username of a player for datastore names and keys. if this person changes his username he will lose all data, use used id instead LisaF854 93 — 6y
0
also, why is the HasM9 inside of game.Players. it should be inside the actual player. LisaF854 93 — 6y
0
Good point, never thought of a player changing their username, but I still use their ID instead. hiimgoodpack 2009 — 6y
0
How ahould it be labeled then? I did put ot inaide the Players section, is that not how i call it? Never2Humble 90 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

First of all, you need to specify EXACTLY what is going wrong. Is it not saving? saving the wrong variable? etc. But this time, dont worry because i figured it out myself.

And yes as Luistheinge said, Hasm9 shouldn't be in the players. Why? Heres Why

Pretend Player 1 just bought the M9, but player2 doesn't have it. Player one the changes that variable now Hasm9 is set to true. Player2's script checks if Hasm9 is there when her dies. Oh,, it says yes, becausePlayer 1 set it to yes.

tl;dr you can't use ONE variable to keep track of MULTIPLE People

Where should it be then? you asked. You can put it in any of the folders that copies stuff and puts it into the INDIVIDUAL player. i.e StarterCharacterScripts, StarterPlayerScripts, StarterGui

FIXED CODE:

--SHOP PURCHASE CODE

--Note: I am putting the value in the player's playergui. Change it if you want.

 tool:Clone().Parent = plr.Backpack 
            tool:Clone().Parent = plr.StarterGear 
            plr.leaderstats.Points.Value = plr.leaderstats.Points.Value - amount  
            game.StarterGui.HasM9.Value = true 

            --The next line is included because StarterGui isn't what the players current GUI is (PlayerGUI), it is what Gui should be loaded into the player each time they die. We have to change both.
            game.Players.LocalPlayer.PlayerGui.HasM9.Value = true

Now, Lets talk about the next problem... You never saved using datastores the m9 value XD. All you did when the player loads, sees if it has it, and gives it if the bool value is right. Pretty silly mistake. Now, heres another problem that ties in..

Look at this line of code when you look if he has the m9... player:GetAsync(player.userId) that is absolute ly wrong, you can't look at the player, then randomly pull the bool value out of no where. Go to the roblox wiki and read how to use Get async.

FIXED CODE:

--DATA STORE CODE


game.Players.PlayerAdded:connect(function(player)
        local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

    player:WaitForChild("leaderstats")
    wait(1)
    local stats = player:FindFirstChild("leaderstats"):GetChildren()
    for i = 1, #stats do            
    stats[i].Value = datastore:GetAsync(stats[i].Name)
    print("stat number "..i.." has been found")

--  FIXED CODE TO GIVE THE M9 (with fixed get async)
local DataStorem9 = game:GetService("DataStoreService"):GetDataStore("HasM9")

    local HasM9 = DataStorem9:GetAsync(player.userId) 

    if HasM9 = true then
    game.ServerStorage.ToolStorage.M9:Clone().Parent = player.Backpack
end

--MY CODE TO SAVE M9

player.PlayerGui:WaitForChild("HasM9").Changed:Connect(function()
if player.PlayerGui.HasM9.Value = true then
local DataStorem9 = game:GetService("DataStoreService"):GetDataStore("HasM9")
DataStorem9:SetAsync(player.userId, true)
end
end)


    stats[i].Changed:Connect(function() 
local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
for i =  1, #statstorage do
    datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
    print("saved data number "..i)

end
print("Stats successfully saved")   
end)
    end

end)
0
:D Good sir. I am going to go try this right now. If it works as well as it should, I do believe I owe you eleven Christmas turkeys. Why eleven? Because it makes as much sense as my broken scripts that you've probably fixed here! Thank you in advance. Never2Humble 90 — 6y
0
Having a problem with one line on the Data Store code - 19 if HasM9 = true then -- "Expected 'then' got '=' " That one error appears to be handing up the script. I've re-worded it a few times and it still errors. Your thoughts? :) Never2Humble 90 — 6y
0
It also gives me the error: 'then' expected near '=' Never2Humble 90 — 6y
1
oh whoops XD put "==" instead of "=" my bad User#17125 0 — 6y
View all comments (7 more)
0
XD Ha! Thank you sir! That fixed that script. ...although my weapons still aren't saving, but I believe the problem now is my Global Death Script, as that is where the error appears... Never2Humble 90 — 6y
0
Drat. Now the stupid thing is saying "HasM9 is not a valid member of PlayerGui" on line 17. For the line: player.PlayerGui.HasM9.changed:Connect(function() Never2Humble 90 — 6y
0
As i said in my answer, you have to move the hasm9 from players to starter Gui User#17125 0 — 6y
0
I did do that. o.o And the value changes to True after being purchased. (btw, I really appreciate you taking the time to help me with this. Its very much appreciated.) Never2Humble 90 — 6y
1
Ok, try the code that i updated User#17125 0 — 6y
0
Thanks for the response an update! It breaks on Line25: player.PlayerGui:WaitForChild("HasM9").Changed:Connect(function() The error I get is "Infinite yield possible on 'Players.Never2Humble.PlayerGui:WaitForChild("HasM9")' When I reset the character, the M9 is no longer in the Backpack, but if I try to buy the weapon from the Shop I get the prompt that its already been purchased. If I leave Never2Humble 90 — 6y
0
the game and return, the item can be purchased again, same Error for that Line25 either way. >< Thanks again for your patience good sir. I know its close to being finished! Never2Humble 90 — 6y
Ad

Answer this question