I'm trying to make it so when a stat changes, it saves. It isn't s stupid leaderstat. It's a IntValue inside a Gui. This is what I have so far; it doesn't work at all. I'm new to datastore and personally think it's stupid. I was just trying to save the ki value but still added the stamina and attack for later.
There are no errors. The problem is that the Ki value won't save
--Btw this is a script in Workspace local DataStore = game:GetService("DataStoreService"):GetDataStore('SSJClickerSaves') game.Players.PlayerAdded:connect(function(Player) local PlayerId = 'id-'.. Player.userId local Ki = Player.PlayerGui.Stats.StatsFrame.Ki:FindFirstChild("KiVal") local Attack = Player.PlayerGui.Stats.StatsFrame.Physical:FindFirstChild("AttackVal") local Stamina = Player.PlayerGui.Stats.StatsFrame.Stamina:FindFirstChild("StamVal") local Save = DataStore:GetAsync(PlayerId) if Save then Ki.Value = Save[1] local SaveValues = {Ki.Value} DataStore:SetAsync(PlayerId, SaveValues) end end) game.Players.PlayerRemoving:connect(function(Player) local PlayerId = 'id-'.. Player.userId local SaveTable = {Player.PlayerGui.Stats.StatsFrame.Ki.KiVal.Value} DataStore:SetAsync(PlayerId, SaveTable) end)
This is for a DBZ game I'm making. It's in the pre-est of pre alphas.
First things first, your code will not support FilteringEnabled
Do not access the Player's PlayerGui in a Server-Script
this will throw errors.
-- in a localscript under StarterGui
or StarterPlayer.StarterPlayerScripts
-- Variables local DataStoreService = game:GetService("DataStoreService") -- Get Service local DataStore = DataStoreService:GetDataStore("SSJClickerSaves") -- Get DataStore local Player = game.Players.LocalPlayer -- Get LocalPlayer i.e Player local Key = "user_" .. Player.UserId -- Get the UserId local PlayerGui = Player.PlayerGui -- Access PlayerGui local Stats = PlayerGui.Stats -- Get Stats local SFrame = Stats.StatsFrame -- Get StatsFrame local Ki = SFrame.Ki:FindFirstChild("KiVal") -- Get Ki local Attack = SFrame.Physical:FindFirstChild("AttackVal") -- Get Attack local Stamina = SFrame.Stamina:FindFirstChild("StamVal") -- Get Stamina -- Function local function updateValues() -- This function will update the DataStore with the new Values DataStore:UpdateAsync(Key, function(oldSave) local newSave = { ["Ki"] = Ki.Value; ["Stamina"] = Stamina.Value; ["Attack"] = Attack.Value; } return newSave end) end -- Events // Activate the updateValues function when the Values of Ki, Attack, and Stamina change Ki.Changed:connect(updateValues) Attack.Changed:connect(updateValues) Stamina.Changed:connect(updateValues)