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!
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)