Cache not assigning properly

local DataStoreService = game:GetService("DataStoreService")
local SpawnStore = DataStoreService:GetDataStore("SpawnPosition")
local SpawnHandler = require(game.ReplicatedStorage.SpawnHandler)
game.Players.PlayerAdded:Connect(function(Player)
	local success , err = pcall(function()
		return SpawnStore:GetAsync(Player.UserId)
	end)
	
	if success then

		Player.Character:SetPrimaryPartCFrame(CFrame.new(Vector3.new(table.unpack(SpawnStore:GetAsync(Player.UserId)))))
		SpawnHandler.PositionCache[Player.UserId] = Vector3.new(table.unpack(SpawnStore:GetAsync(Player.UserId)))
		print(SpawnHandler.PositionCache)
	else
		warn(err)
		local Pos = {-12 , 4.6 , 16}
		SpawnStore:SetAsync(Player.UserId , Pos)

		Player.Character:SetPrimaryPartCFrame(CFrame.new(Vector3.new(table.unpack(SpawnStore:GetAsync(Player.UserId)))))
		SpawnHandler.PositionCache[Player.UserId] = Vector3.new(table.unpack(SpawnStore:GetAsync(Player.UserId)))
	end
end)

Result:
{
[157354373] = -12, 4.5999999, 16
}

On a localscript:

print(SpawnHandler.PositionCache)

result:
{}

Why though

I think it’s because you’re only setting the variable once. Once you set a variable and assign something to it, it won’t change whatevers assigned to it, it’ll stay with it’s original data, even if you change the value, etc. For example:

local Value1 = script.Parent.Value -- Imagine this value is "1"

script.Parent.Value = 2 -- Here we will change the value.

print(Value1) -- You would assume this will print "2" but it'll print "1" because once you assign a variable, it's stays in it's original state until you re-assign it. (the Value1 variable.)

Let me know if i’m wrong. :slightly_smiling_face:

The local script prints spawnchache several seconds after the serverscript sets it so i don’t think that’s where the problem lies.

1 Like

cuz you’re returning the async of the async so err’s basically the async of the data store, remove the return bro

Oh, I see what you mean.
Probably because of how .PlayerAdded scripts for local scripts, it’ll only work for when other players joins the game, because the script only loads after your player gets added.

Or… I’m just messing up the whole thing here haha

What do you mean?
Shouldn’t that return the value asociated with the key?
After all, this does work, the cache prints successfully on the server but not on the client.

So you’re saying that the changes don’t replicate to the client?

it is returning the array in the key y’know
thats why the err’s the thingy stored in the key of the data store

without the return it’d catch the error

pretty sure when you placed code to be returned it actually runs, and that the only way to return a function is to set it as a variable and return the variable.

If you’re using a local script for .PlayerAdded, the localplayer cannot run the event themselves because the script only loads after the player is added which means the event will only run when another player joins, since your side of the script has loaded after you joined, it won’t run for you because your local script loads after your player. That’s why it only works for serverscript, because it’s already loaded from the server.

remove the return thingy so the pcall will catch the actual error
also delete your data, otherwise it’ll keep doing the error
cuz it is saved already

No, the first code sample is in a server script, my issue is that the changes don’t seem to replicate to the client automatically

oh ok mb

but the print thingy’s on the local script and the server one?

the second code sample is in a localscript, that one prints an empty table

This seems to be a client/server boundary issue if it is printing properly on the server but not on the client. I just now tested it and I was correct, the client cannot access changes to module scripts from the server.

Instead try using int values for the playerId and a vector3Value as the playerId’s child OR you can use a remote function/ event to retrieve the position(remote function more practical for this situation if called from the client)

1 Like