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

Script Not Getting the Correct Value From Variable?

I originally thought this was an issue with datastore, but upon further examination I determined that the data was saving, but the Rubles value from the player removing function is getting the wrong Rubles Value. For instance, when I set the Rubles value to 25, and then I click stop, even though the Rubles Variable is 25, the script still outputs 10 when printing savetable. The reason I know this to be true is because I edited the line "local Savetable = {plr.leaderstat.Rubles.Value}" to "local Savetable = {plr.leaderstat.Rubles.Value+2}". And next time I load the game, I have 12. So I'm wondering why incorrect data is being retrieved here.

local DataStoreService = game:GetService('DataStoreService')
local moneyStore = DataStoreService:GetDataStore('Djmaster444TestStore03')
game.Players.PlayerAdded:connect(function(plr)
	plr:WaitForChild(plr.Backpack.Name)
	-- Define variables
	local uniquekey = 'id-'..plr.userId
	local leaderstat = Instance.new('NumberValue', plr)
	local savevalue =  Instance.new('NumberValue')
	leaderstat.Name = 'leaderstat'
	savevalue.Parent = leaderstat
	savevalue.Name = 'Rubles'
	savevalue.Value = 10.00
	
	-- GetAsync
	local GetSaved = moneyStore:GetAsync(uniquekey)
	print(GetSaved[1])
	if GetSaved then
		savevalue.Value = GetSaved[1]
	else
		local NumbersForSaving = {savevalue.Value}
		moneyStore:SetAsync(uniquekey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	saveData(plr)
end)

function saveData(plr)
	local uniquekey = 'id-'..plr.userId
	local Savetable = {plr.leaderstat.Rubles.Value}
	moneyStore:SetAsync(uniquekey, Savetable)
end

1 answer

you could try my datastore model: https://www.roblox.com/library/2790189297/Datastore Heres how it works

--You must publish the game for it to work
local Mod = require(game.ServerStorage.StatsHandler)
Mod:GetFolderName("leaderstats")
Mod:GetStarterData({Rubles = 10})

game.Players.PlayerAdded:Connect(function(plr)
	Mod:LoadStarterData(plr)
	Mod:LoadData(plr)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	Mod:SaveData(plr)
end)

5 comments

0

I appreciate the help, but I'd also like to know the data is not saving.

0

You can go check my module to check out how it works

0

or check out here https://pastebin.com/VxSNp0cV It has pcalls because there might be a error while loading/saving data so the pcall wont error and the script wont break down

0

It's not datastore. I did some diagnosing, and I've realized that savetable is not reading the the "Rubles" value correctly. I'm going to edit the question to accommodate this.

0

Oh ok

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