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

Why is my datastore not saving the int value?

I have it making and saving a integer value inside a TextLabel inside a ScreenGui in the PlayerGui but it wont save it. Here's the code:

local DSService = game:GetService('DataStoreService'):GetDataStore('Hamburger223232')
game.Players.PlayerAdded:connect(function(plr)
	-- Define variables
	local uniquekey = 'id-'..plr.userId
	local leaderstats = Instance.new('IntValue', plr)
	local savevalue =  Instance.new('IntValue')
	
	savevalue.Parent = game.StarterGui.ScreenGui.TextLabel
	savevalue.Name = 'Bounty'
	
	-- GetAsync
	local GetSaved = DSService:GetAsync(uniquekey)
	if GetSaved then
		savevalue.Value = GetSaved[1]
	else
		local NumbersForSaving = {savevalue.Value}
		DSService:SetAsync(uniquekey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local Savetable = {plr.PlayerGui.ScreenGui.TextLabel.Bounty.Value}
DSService:SetAsync(uniquekey, Savetable)	
	
	
end)

1 comment

0

Are you testing it in studio??

1 answer

There are a few things that aren't happening right here.

Firstly, you were parenting it to StarterGui, meaning that:

  1. It will replicate to every player
  2. It will stack with previously created values because StarterGui doesn't remove anything

Secondly, you'd want to parent savevalue to somewhere more persistent than PlayerGui (stuff from StarterGui gets cloned into a player's PlayerGui) - by default it resets when the characters loads, so you will have to re-make the value every time the character spawns.

Instead, I recommend you parent it to somewhere that doesn't reset (like the player object, or the leaderstats object if you want it to show on the leaderboard).


Thirdly, the code from lines 15-17 that saves NumbersForSaving is useless because it is essentially saving (and therefore loading) exactly the same value as is the default value when the value object gets created.

To show instead of tell:

Loaded/SavedValue = 0
DefaultValue = 0

Finally, just to update your saving system, change the value it indexes on line 23 from what it currently is to plr.leaderstats.Bounty.Value.


Final code:

local DSService = game:GetService('DataStoreService'):GetDataStore('Hamburger223232')
game.Players.PlayerAdded:connect(function(plr)
    -- Define variables
    local uniquekey = 'id-'..plr.userId
    local leaderstats = Instance.new('IntValue', plr)
    local savevalue =  Instance.new('IntValue')

    savevalue.Parent = leaderstats
    savevalue.Name = 'Bounty'

    -- GetAsync
    local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        savevalue.Value = GetSaved[1]
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local uniquekey = 'id-'..plr.userId
	local Savetable = {plr.leaderstats.Bounty.Value}
	DSService:SetAsync(uniquekey, Savetable)    
end)

Tip: Format your code using Tabs and not Spaces.


Hope I could help!

~TDP

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