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

Datastore stuff not saving or loading?

This script gives NO errors in the output! But it doesn't save the Coins or the Level or the Reward. I've made 2 scripts, one's the Load script and one's the Save script. I'm just starting to use some datastores so it might have alot of errors!

Load script:

levl = game:GetService("DataStoreService"):GetDataStore("LevelTesting")
cahs = game:GetService("DataStoreService"):GetDataStore("CashTesting")
rewa = game:GetService("DataStoreService"):GetDataStore("DRewardTesting")
game.Players.PlayerAdded:connect(function(plr)
	local key = "save-"..plr.userId	
	
	local stats = Instance.new("Model", plr)
	stats.Name = "leaderstats"
	
	local L = Instance.new("IntValue", stats)
	L.Name = "Lvl"
	
	local C = Instance.new("IntValue", stats)
	C.Name = "Coins"
	
	local r = Instance.new("BoolValue", plr)
	r.Name = "Reward"
	
	local saves = {levl:GetAsync(key), cahs:GetAsync(key), rewa:GetAsync(key)}
	
	if saves ~= nil then
		L.Value = saves[1] or 1
		C.Value = saves[2] or 0
		r.Value = saves[3] or false
	end
end)

Save script:

levl = game:GetService("DataStoreService"):GetDataStore("LevelTesting")
cahs = game:GetService("DataStoreService"):GetDataStore("CashTesting")
rewa = game:GetService("DataStoreService"):GetDataStore("DRewardTesting")

game.Players.PlayerRemoving:connect(function(plr)
	local key = "save-"..plr.userId	
	
	levl:SetAsync(key, plr.leaderstats.Lvl.Value)
	cahs:SetAsync(key, plr.leaderstas.Lvl.Value)
	rewa:SetAsync(key, plr.Reward.Value)
end)

Idk whats wrong after all there's no errors.

both the scripts are NORMAL scripts, not local in ServerScriptService

The save script doesn't seem to me saving OR the load script doesn't seem to be loading.

1 comment

0

Does the script give errors in-game in the dev-console under server scripts? (Join your game and push F9)

1 answer

Your scripts appear to be fine. I am not sure about your use of or for your Loading script on line 22-24.

Anyway, I assume your game is having no one in it instead of you, so when you leave, your game is possibly shutting down before the saving can be finished for Data Stores! We would fix this by using OnClose().

Please go to this link for information for OnClose!

  • Other Possible Problems

Another problem could be checking if the table is not nil. Instead, try to check if the contents are not nil ( if save[1] ~= nil then).

If other problems occur, I would edit line 22-24's uses of or. I do not know if that would work. An example edit for line 22 would be

if save[1] == nil then
	L.Value = 1 -- I believe this is what you mean to be occurring if nil
elseif save[1] ~= nil then
	L.Value = save[1]
end

I hope this helps, be careful with Data Stores! Very simple things could be the cause of a data problem!

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