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

DataStore being generally confusing. Help, anyone?

The expected result of the SaveData() function in the code below is to print "Saved data for Player1." However, it NEVER prints this. I'm having trouble because this also occurs when I replace UpdateAsync() with SetAsync(). No error is returned either.

Here's the whole script:

local DataManager = {}

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

local SessionData = {} -- Current game data
local StartingData = -- Data for players who have never played.
{
	Cash = 0, 
	Experience = 0, 
	Level = 0, 
	TotalDamage = 0, 
	EquippedWeapon = "Stick", 
	Weapons = {"Stick"}
} 

local function SaveData(Player)
	
	local PlayerId = Player.UserId
	local Data = SessionData[PlayerId]
	
	print(Player.Name .. " left. Saving data.")
	
	if Data then 
		
		local success, err = pcall(function()
			PlayerData:UpdateAsync(PlayerId, function(old) return Data end)
		end)
	
		if not success then print(err) error("Cannot save data for " .. Player.Name .. "!") end
		
	end
	
	print("Saved data for " .. Player.Name .. ".")
	
end

game.Players.PlayerRemoving:Connect(function(Player)		
	SaveData(Player)
end)

return DataManager

9 comments

0

I'm guessing this is not the only code, you also missed the first and last line in the codeblock. To begin if there are no errors it's either working or not running, try printing something on line 1 to see if it is. If it's not running it's either disabled or misplaced.

0

I've posted the whole script. I can't seem to find the problem, as it runs fine otherwise.

0

Why is it so spaced out >.<

0

How I code man, I don't like big clumps of text. Same reason they alternate colors on charts

0

Can you at least cut your code into sections and tell us what each of those sections do? There's a lot of things to read and saving some time means I could also be helping someone else.

0

This depends are you testing this in studio or on a real server?

0

Will cut code and using APS, so it shouldn't matter @Syntax

0

Cut the code. I left in the code that I have the problem with

0

Why use UpdateAsync when you're using it like you'd use SetAsync? SetAsync overrides the current data, which is what you're already doing

1 answer

There is a simpler way of doing it... Here: (Hope it helps!!)

local Service = game:GetService('DataStoreService'):GetDataStore('CHANGE_THIS')--Change whats inside the '' not the variable
game.Players.PlayerAdded:connect(function(plr)
	local stats = plr:WaitForChild('leaderstats')
	local Exp = stats:WaitForChild('Exp')--Change whats inside the '' not the variable
	local Level = stats:WaitForChild('Level')--Change whats inside the '' not the variable
	local uniquekey = 'id-'..plr.userId
	local GetSaved = Service:GetAsync(uniquekey)
	if GetSaved then
		Exp.Value = GetSaved[1]
		Level.Value = GetSaved[2]
	else
		local SavingNums = {Exp.Value,Level.Value}
		Service:SetAsync(uniquekey,SavingNums)
	end
end)
game.Players.PlayerRemoving:connect(function(plr)
	local stats = plr:WaitForChild('leaderstats')
	local Exp = stats:WaitForChild('Exp')--Change whats inside the '' not the variable
	local Level = stats:WaitForChild('Level')--Change whats inside the '' not the variable
	local uniquekey = 'id-'..plr.userId
	local Savetable = {Exp.Value,Level.Value}
	Service:SetAsync(uniquekey,Savetable)
end)--Hope this helps you out!

Enjoy.

1 comment

0

If you want it to give weapons when you unlock them then just give me a shout.

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