Kampfkarren / Roblox

Scripts and stuff I wrote for Roblox. Documentation is little to none as these are just stuff I took from my game that I thought I could share.
https://kampfkarren.github.io/Roblox/
Other
278 stars 76 forks source link

On startup, data is not there, so players are starting over. #29

Closed visskiss closed 5 years ago

visskiss commented 5 years ago

Relevant sections are

Inside ServerScriptingService is this script

player.CharacterAdded:Connect(function(character)
    wait()
    local checkpointStore = DataStore2("checkpoints", player)
    local playerCheckpoint = checkpointStore:Get()
    if playerCheckpoint then
        teleportCharacterToCheckpoint(character,playerCheckpoint)
    end
end)

function teleportCharacterToCheckpoint (character,checkpointNumber)
print("Teleporting to "..tostring(checkpointNumber))
local checkpointPart = CheckpointManager:findCheckpointNumber(checkpointNumber) 
if checkpointPart then

    teleportCharacterToCFrame(character,checkpointPart.CFrame)
end
end

everything was working fine until datastore2, which now seems to be losing the initial get...

Kampfkarren commented 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.

visskiss commented 5 years ago

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?

visskiss commented 5 years ago

By the way, it happens the first time after a time away.

Kampfkarren commented 5 years ago

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.

visskiss commented 5 years ago

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?

visskiss commented 5 years ago

The thing is, none of that timing really changed. Maybe DataStore2 delays script loading to push it past the playerAdded?

Kampfkarren commented 5 years ago

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?

Kampfkarren commented 5 years ago

As for the PlayerAdded thing, the fix is usually:

Players.PlayerAdded:connect(playerAdded)
for _, player in pairs(Players:GetPlayers()) do
    playerAdded(player)
end
visskiss commented 5 years ago

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)
visskiss commented 5 years ago

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.

Kampfkarren commented 5 years ago

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().