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

Why isn't this data store saving? The Pcall says it is working

local ds = game:GetService("DataStoreService")
local player = game:GetService("Players")
local rep =  game:GetService("ReplicatedStorage")
local statsf = rep:WaitForChild("Stats")
local datastore = ds:GetDataStore("Stats")
local function saveData(player) 
	local speed = statsf:WaitForChild(player.Name):WaitForChild("Speed")
	local strength = statsf:WaitForChild(player.Name):WaitForChild("Strength")
	local data = {
		speed.Value,
		strength.Value
	}

	local success, err = pcall(function()
		datastore:SetAsync(player.UserId, data) 
	end)

	if success then
		print("Data has been saved!")
	else if err then
		print("Data hasn't been saved!")
		warn(err)		
		end
	end
end

player.PlayerAdded:Connect(function(plr)
		local userID = plr.userId
		local stats = Instance.new("Folder")
		wait (.1)
		stats.Parent = statsf
		stats.Name = plr.Name

		local Speed = Instance.new("IntValue")
		Speed.Name = ("Speed")
		Speed.Parent = stats

		local Strength = Instance.new("IntValue")
		Strength.Name = ("Strength")
		Strength.Parent = stats
		local data
	local success, err = pcall(function()
		data = data:GetAsync(player.UserId)

	end)
	if success then 
		Speed.Value = data[1]
		Strength.Value = data[2]
	elseif err then print ("No data found")
		Speed.Value = 12
		Strength.Value = 0
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err  = pcall(function()
		saveData(player) 
	end)

	if success then
		print("Data has been saved")
	elseif err then
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() 
	for _, player in pairs(game.Players:GetPlayers()) do 
		local success, err  = pcall(function()
			saveData(player)
		end)

		if success then
			print("Data has been saved")
		elseif err then
			print("Data has not been saved!")
		end
		end
		end)

Also, if there is any way to improve upon this script, let me know! Thank you

1 answer

✓ Accepted answer

I think you are trying to save a table. Try to do this on line 14 to 16:

local success, err = pcall(function()
	datastore:SetAsync(player.UserId.. " speed", data[1]
	datastore:SetAsync(player.UserId.. " strength", data[2]
end)

Since you used the same key, and saved strength after speed, so your speed key will overlapped by the strength value (i think this is one of the problem, though), so you should consider use 2 different keys (your data will reset, sorry lol)

1 comment

0

thanks for the quick reply! I have just swapped over to datastore2

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