Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to save a gui value using datastore??

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.

1 answer

✓ Accepted answer

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)

3 comments

0

Although your script didn't quite work, I appreciate all the effort. Also, this gave me a better idea of how the dataStore works <3

0

no problem, good luck!

0

I would not use a changed event to save player stats, this will exceed the limitations.

This discussion is preserved as it was. New answers, comments, and votes are closed.