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

Data Store script not saving values? [Help!]

This data store script doesn't work for some reason. Can you guys check it out please. I have put print() after if nil then and it did not print, this means that the data store is saving but it's not saving the right value.

Script location = ServerScriptService

local xp = game:GetService("DataStoreService"):GetDataStore("XP")

local function Xp(key, newValue)
  	local suc, res = pcall(function()  return xp:SetAsync(key, newValue) end)
	if suc then	
		return true
	end
end

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local experience = Instance.new('IntValue')
  		experience.Name = 'XP'
  		experience.Value = xp:GetAsync(player.UserId)
		if nil then
			-- I put the print() in here
			experience.Value = 0
		end
		while wait(30) do
			Xp(player.UserId, player.XP.Value)
		end
	end)
end)

1 answer

✓ Accepted answer

You forgot to set the parent, I gave some modified. errors: Parent not set. You can not put if nil then you must specify the variable / object. You do not need to use player.CharacterAdded. It is not necessary to create a save function.

If it does not work in Roblox Studio you must enable the API, go to the page of your place, go to Configure Game and enable Studio Access to API Services, click save and that's it.

Here is the script done:

local xp = game:GetService("DataStoreService"):GetDataStore("XP")

local startvalue = 0 -- Set start XP

game:GetService('Players').PlayerAdded:Connect(function(player)
	local async = xp:GetAsync(player.UserId) -- Get Saved Value
	local leaderstats = Instance.new('IntValue', player) -- Create a leaderstats
	leaderstats.Name = 'leaderstats'
	local experience = Instance.new('IntValue', leaderstats) -- Create XP value
	experience.Name = 'XP'
	if async then -- If found save then
		experience.Value = xp:GetAsync(player.UserId) -- Get XP Saved
	else
		experience.Value = startvalue -- Set start value
		wait()
		xp:SetAsync(player.UserId, experience.Value) -- Save XP
	end
	experience.Changed:Connect(function() -- If XP changed save value
		xp:SetAsync(player.UserId, experience.Value) -- Save XP
	end)
end)

If you want to make some changes you can do, however it is not recommended to add a loop to save the value.

Remember, for save you need to change value with server, for this use RemoteEvent

11 comments

0

I actually do need player.CharacterAdded, because i have other data stores i just took one of them out of my script

0

Create a new script and datastore key to store the other value.

0

if you want me try to fix, send all code. with another data store

0

I disabled my Data Store script and pasted yours in a new script, it didn't work

0

what error? (remember if you using "XP" data store on two scripts, you need to change the key in one of the two)

0

Remember, only can save the values if server change. if the client change not save anything, for this use Remote Event or Server Script.

0

No Error

0

If no error you need to change the value with server, For this use RemoteEvent or Server Script.

0

Done that didn't work

0

I do not know then, because I tried the script and it works perfectly for me. (If you already used the DataStore "XP" change for "XP-0" or anything number...)

0

I figured it out It was that i wasn't changing the value on the server side but i thought i did, but i guess not (btw i tested with my original code and it works perfectly)

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