Closed visskiss closed 5 years ago
By "Everything was working fine until DataStore2", what do you mean? Were you using normal data stores before, and just switched over? If so, normal data store data is not transferable to DataStore2.
No, I did not expect the data to transfer. I mean that the initial load always worked. Now, I noticed in studio, I'll start from zero. And also in the regular version, my daughter reports starting from scratch. It is possible that it's an issue with the character added timing?
By the way, it happens the first time after a time away.
The character adding timing seems fine to me (other than the wait()) is not needed. If the key was one that used to exist, try replacing it with a new one. In studio by default, data doesn't save (you'll get a warning in the output about it).
When/how do you set their data? Your code snippet doesn't show that.
Here's the save, which happens on touch of checkpoint.
function PlayerDataManager:savePlayerCheckpoint(player,checkpointNumber)
local checkpointStore = DataStore2("checkpoints", player)
print("set checkpoint for "..key(player).." to "..tostring(checkpointNumber))
checkpointStore:Set(checkpointNumber)
end
The character added full function is:
players.PlayerAdded:Connect(function (player)
print(tostring(player.userId).." added")
player.CharacterAdded:Connect(function(character)
wait()
local checkpointStore = DataStore2("checkpoints", player)
local playerCheckpoint = checkpointStore:Get()
if playerCheckpoint then
teleportCharacterToCheckpoint(character,playerCheckpoint)
end
end)
local function skipsUpdated(skipCount)
local replicatedStorage = game:GetService("ReplicatedStorage")
local skipsUpdatedNotify = replicatedStorage:WaitForChild("SkipsUpdatedNotify")
skipsUpdatedNotify:FireClient(player,skipCount)
end
local skipStore = DataStore2("skips",player)
skipStore:OnUpdate(skipsUpdated)
skipsUpdated(skipStore:Get(1))
end)
My thought now is that playerAdded is not being called initially. This is from Roblox
Players.PlayerAdded : This event does not work as expected in solo mode, because the player is created before scripts that connect to PlayerAdded run. To handle this case, as well as cases in which the script is added into the game after a player enters, create an OnPlayerAdded function that you can call to handle a player’s entrance.
Perhaps I should try to call the on player added function when the script is loaded as well?
The thing is, none of that timing really changed. Maybe DataStore2 delays script loading to push it past the playerAdded?
Maybe DataStore2 delays script loading to push it past the playerAdded? Calling :Get() yields, but you seem to be running DataStore2 functions after connecting everything (and I use DataStore2 in both CharacterAddeds and PlayerAddeds).
Can you check your output to see if there's any warnings/errors?
As for the PlayerAdded thing, the fix is usually:
Players.PlayerAdded:connect(playerAdded)
for _, player in pairs(Players:GetPlayers()) do
playerAdded(player)
end
The studio issue seems to be isolated to studio, so I am not worried for now. However, when you noted above that the wait() command is not necessary, I removed it and noticed that on death (which really only happens rarely) that the CharacterAdded does not in fact work.
In other words this
player.CharacterAdded:Connect(function(character)
local checkpointStore = DataStore2("checkpoints", player)
local playerCheckpoint = checkpointStore:Get()
if playerCheckpoint then
teleportCharacterToCheckpoint(character,playerCheckpoint)
end
end)
doesn't work
but this does
player.CharacterAdded:Connect(function(character)
wait()
local checkpointStore = DataStore2("checkpoints", player)
local playerCheckpoint = checkpointStore:Get()
if playerCheckpoint then
teleportCharacterToCheckpoint(character,playerCheckpoint)
end
end)
My workaround so far is to move the call (teleportToSavedCheckpoint) to a remote event that gets called by a local script in starterCharacterCcripts.
This seems to work without issue.
That's a weird fix, but as long as it's not a DataStore2 issue.
As for wait(), try replacing it with game:GetService("RunService").Heartbeat:Wait()
.
Relevant sections are
Inside ServerScriptingService is this script
everything was working fine until datastore2, which now seems to be losing the initial get...