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

[Solved itself] Datastore problem with saving/loading, any ideas how to fix this?

Alright, so I've recently found a problem with loading my data. If I rejoin the game with another player present, it will just load his size and his cash. I'm not 100% sure if the problem occurs when saving or loading.

local service = game:GetService("DataStoreService")
local datastore = service:GetDataStore("PlayerSize4")


game.Players.PlayerAdded:Connect(function(plr)


	


	local datatoload = {}
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local size = Instance.new("IntValue")
	size.Name = "Size"
	size.Parent = leaderstats

	
		local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats

	
	local success,errorm = pcall(function()
		 datatoload = datastore:GetAsync("Size-"..plr.UserId)
	end)

	if success then
if datatoload ~= nil then
		plr.leaderstats.Size.Value = datatoload[1]
			plr.leaderstats.Cash.Value = datatoload[2]
				print("Data loaded")
			end
	else
		error("Data could not be loaded.")
	end

end)






game.Players.PlayerRemoving:Connect(function(player)
	local datatosave = {}
	datatosave[1] = player.leaderstats.Size.Value
	datatosave[2] = player.leaderstats.Cash.Value
	local success,errorm = pcall(function()
    datastore:SetAsync("Size-"..player.UserId, datatosave)
	end)

	if success then
		print("Data saved")
		
	else
		error("An error occured while saving")
	end

end)

2 comments

0

what is player2 on line 4?

0

Just something I forgot to remove. But that's sadly not the problem.

2 answers

Have you enabled access to API Services for your studio? Click Game Settings on home tab, then publish if it requers, click Security and enable this thing

5 comments

0

Yes. I've already done that.

0

The real problem is that if another player is online HIS data gets loaded instead of yours if you rejoin the game. It could also be that if you rejoin that somehow the others player data gets saved as yours etc.

0

Ok let me test it, I want to locate this magic on my plc

0

If you mean to load 2 players on roblox web not in the studio then I cant recreat this, I dont have second account, sorry.

0

It’s fine. The problem fixed itself. Take a look at my answer down below. Thanks for helping me out though!

Huh well, the problem seems to have solved itself. I've tried it out again and now everything's working smoothly as it should. Sorry to bother you!

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