I’m making an obby and saving check points using Profile Service. The problem is the code is messy and uses a lot of if statements.
I was wondering how this could be cleaned up. Also, I don’t know if I need to coroutine the second function, please let me know if I need to. By the way these two parts of the code are in a script in ServerScriptService.
Here is the first part:
for i, Part in pairs(Parts) do -- Iterates through all the check points in my game Part.Touched:Connect(function(Hit) local player = Players:GetPlayerFromCharacter(Hit.Parent) --Checks if the player actually exists if player then local PlayerProfile = ProfileCache[player] -- Checks to see if the player has data if PlayerProfile then if tonumber(Part.name) > PlayerProfile.Data.Stage then -- Makes sure you can't go back a stage/level PlayerProfile.Data.Stage = tonumber(Part.name) -- Updates your profile service data which in turn updates a leaderstat end end end end) end
Second script (its in the same script (TELL ME IF I SHOULD USE A COROUNTINE)):
Players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(char) local HRP = char:WaitForChild("HumanoidRootPart") local PlayerProfile = ProfileCache[player] -- Gets the players Profile Service Data if PlayerProfile then -- Makes sure it exists for i, Part in pairs(Parts) do -- Iterates through all of the checkpoints if tonumber(Part.Name) == PlayerProfile.Data.Stage then -- See's if the data matches the stage wait() -- If I don't add this it doesn't work even though I used WaitForChild() HRP.CFrame = Part.CFrame -- Spawns at check point end end end end) end)