I have a saving script to save various things. I've looked through it multiple times and I can't tell why it won't work. Thanks in Advance!
local Players = game:GetService("Players") local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService:GetDataStore("-xBuckDatax-") -- Making the saving inside a function it shortened your code by a bit. local function Save(player) local key = "Player_"..player.UserId local save = { ["SeerBux"] = player.leaderstats["SeerBux"].Value, ["SpdWaitTime"] = player.WaitTime.WaitTimeSpd.Value, ["StrWaitTime"] = player.WaitTime.WaitTimeStr.Value } --saves their key and money to the data store local success, err = pcall(function() DataStore:SetAsync(key, save) end) if not success then warn("Failed to over-write data"..tostring(err)) return end end -- I made this a function so it can be called whenever local function Load(player) -- saves the player Id as key local key = player.UserId local moneyAlready local success, err = pcall(function() moneyAlready = DataStore:GetAsync(key) end) if not success then warn("Failed to read data"..tostring(err)) return end if moneyAlready then player.leaderstats.SeerBux.Value = moneyAlready.SeerBux player.WaitTime.WaitTimeSpd.Value = moneyAlready.SpdWaitTime player.WaitTime.WaitTimeStr.Value = moneyAlready.StrWaitTime else Save(player) end end Players.PlayerAdded:Connect(Load) Players.PlayerRemoving:Connect(Save)
As far as I know, you can't save multiple values to the same DataStore. You have to manually assign them. I can't really tell what you're trying to save but try something like this
game:GetService("Players") game.Players.PlayerAdded:Connect(function(newPlayer)
local DataStore = game:GetService("DateStoreService") local Money = DataStore:GetDataStore("PlayerMoneyService") --just a save value for instance
you could use
v.Value = Money:GetAsync(newPlayer.UserId) or 100
and then you could set it with
local x = 250 -- whatever money the player has in the game
Money:SetAsync(newPlayer.UserId, x.Value)
x.Changed:Connect(function() Money:SetAsync(newPlayer.UserId, x.Value) end)
end)
hope this helps. you might want to make a script that places a number value inside of the player (game.Player) and then use that as x. Then you can use touch scripts or other scripts to change to players money value, or X. When the player's Money value changes the function will be triggered and the datastore will save. Hope this helps
also, I would ditch the function
I redid the code in the studio and the final thing should look like this
game:GetService("Players") local DataStore = game:GetService("DataStoreService") local Money = DataStore:GetDataStore("PlayerMoneyService") --just a save value for instance game.Players.PlayerAdded:Connect(function(newPlayer)
local PMoney = Instance.new("NumberValue", newPlayer) -- makes a value and puts it in the player --(game.Player)
PMoney.Value = Money:GetAsync(newPlayer.UserId) or 100
Money:SetAsync(newPlayer.UserId, PMoney)
PMoney.Changed:Connect(function() Money:SetAsync(newPlayer.UserId, PMoney.Value) end)
end)
changed the players money get the players name from whatever function they triggered and search for them in game.Players by using their name