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

Why did this script to save player data stop working?[UPDATED]

This script worked before but now it won't and i can't figure out why,any input would be appreciated.

Thanks for your time

local data = game:GetService("DataStoreService")
local dataStor = data:GetDataStore("MobbyProgress3")


game.Players.PlayerAdded:connect(function(p)	
	local stats = Instance.new("IntValue",p)
	stats.Name = "leaderstats"
	
	local stage = Instance.new("IntValue",stats)
	stage.Name = "Stage"
	stage.Value = 0
	
	local Trophies = Instance.new("IntValue",stats)
	Trophies.Name = "Trophies"
	Trophies.Value = 0
	
	
	local Trophy1 = Instance.new("BoolValue",p)
	Trophy1.Name = "Trophy1"
	Trophy1.Value = false
	
	
	local Trophy2 = Instance.new("BoolValue",p)
	Trophy2.Name = "Trophy2"
	Trophy2.Value = false
	
	
	local Trophy3 = Instance.new("BoolValue",p)
	Trophy3.Name = "Trophy3"
	Trophy3.Value = false
	
	key = 'MobbyProgress3_'..p.userId
	
	 local saveData = dataStor:GetAsync(key)
	
	if saveData == nil then 	
		stage.Value = saveData[1]
		Trophies.Value = saveData[2]
		Trophy1.Value = saveData[3]
		Trophy2.Value = saveData[4]
		Trophy3.Value = saveData[5]
	else
		dataStor:SetAsync(key, {1,2,3}) 

	end	
		end)



game.Players.PlayerRemoving:connect(function(p) 
	key = 'MobbyProgress3_'..p.userId
	
	local save = {p.leaderstats.Stage.Value, p.leaderstats.Trophies.Value,p.Trophy1.Value,
		p.Trophy2.Value,p.Trophy3.Value,p.Trophy4.Value,p.Trophy5.Value,p.Trophy6.Value,
		p.Trophy7.Value,p.Trophy8.Value,p.Trophy9.Value,p.Trophy10.Value} 

 	dataStor:UpdateAsync(key, save)
end)

1 answer

✓ Accepted answer

The if on line 57 (and corresponding end) should probably be removed -- you should want to save regardless of what's happened before (unless, perhaps, the DataStore failed to load in the first place). Once a player has information saved to a key, GetAsync will never return nil for that key again (since you cannot set it to nil).

Likewise, you don't need line 52, unless you intend to compare it with the current progress (and then keep/save the best one into storage).

Also note that datastore calls are supposed to be wrapped in pcall. Get/SetAsync will occasionally fail and your script should theoretically be able to handle those cases (usually by waiting for a few seconds and trying again for maybe even a minute). Failing to do this will result in some lost data, but it should work a lot of the time.

[Edit/Update]

Line 36 should say if saveData ~= nil then (or you can say if not saveData then, whichever you prefer). You only want to load the data (what lines 37 - 41 are doing) if you have saveData, not if it's nil.

2 comments

0

i've updated my script in the meantime i'll edit it

0

Answer updated

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