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

Can't save/load a table?

local dss=game:GetService'DataStoreService':GetDataStore'Data'

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local getdata=dss:GetAsync(player.UserId)
		if getdata then
			character.HumanoidRootPart.CFrame=CFrame.new(getdata[1],getdata[2],getdata[3])
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local savedata={
		player.Character.HumanoidRootPart.Position.X,
		player.Character.HumanoidRootPart.Position.Y,
		player.Character.HumanoidRootPart.Position.Z
	}
	dss:SetAsync(player.UserId,savedata)
end)

I'm trying to get player's x, y and z positions, store them in a table and load them when player joins with them saved. However, I can't make it work and the output says nothing... I'm very green with data stores and I would very appreciate any help.

2 answers

✓ Accepted answer

Character is printing nil. You could try tracking your character's position using a value instead and update based on that.

game.Players.PlayerRemoving:Connect(function(player)
	local key = string.format("%s_%s", player.Name, player.UserId)
	if player.Character then 
		local root_part = player.Character:findFirstChild("HumanoidRootPart")
		if root_part then 
			if pcall(function()
				dss:SetAsync(key, root_part.Position)
			end) then 
				print(string.format("Saved %s position: %s", player.Name, root_part.Position))
			else
				print(string.format("Couldn't save %s position.", player.Name))
			end
		else
			print(string.format("Couldn't find %s torso.", player.Name))
		end
	else
		print(player.Name, "/Character is nil")
	end
end)

3 comments

0

Thanks for the answer but that doesn't seem to be the problem... :/

0

If it's not saving, it's not going to load. Otherwise there is an error and you should be able to see it in the output. StarterServer > 1 player. Leave the window your character has loaded in, look in server output.

0

Thanks a bunch! I got it to work now c:

I personally don't have much experience working with data stores, but I can offer some advice that might help out.

  1. According the wiki, data store keys should be strings, but you are passing integers (player IDs) instead. Consider using something like "player" .. player.UserId as a key instead.

  2. Keep in mind that a character's default position may be (unfortunately) assigned after the player.CharacterAdded has been fired, in which case, the position you have assigned to the character will be overridden. There really isn't a clean way to deal with this other than to wait a brief period of time before assigning the character's position so that the character's default position can be assigned first.

  3. Calling GetAsync every time a player's character loads is inefficient. You should only call it once when the player joins the game and then store its value in a variable for later use.

  4. Calls to data store functions can fail due to networking issues. See here for a better explanation.

Hope this helps.

1 comment

0

I still can't make it to work. Thanks for the point one though, I didn't knew that. I'm pretty sure the problem is at lines 5-8 because if I add a print after line 8 it won't print it...

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