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

Checkpoint system not working properly?

I have a checkpoint system relying on spawnlocations, however when the players join the game, the player spawns a random checkpoint instead of the intended checkpoint.

ServerScriptStorage

ServerStorage = game:GetService("ServerStorage")

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(c)

if ServerStorage:FindFirstChild('CheckPointFolder') then
else

	local Folder = Instance.new('Folder')
	Folder.Name = 'CheckPointFolder'
	Folder.Parent = game:GetService('ServerStorage')
end
	if ServerStorage.CheckPointFolder:FindFirstChild(p.UserId) then
else
	local PlayerStats = Instance.new('Folder')
	PlayerStats.Name = p.UserId
	PlayerStats.Parent = ServerStorage:WaitForChild('CheckPointFolder')

	local CheckPointData = Instance.new('ObjectValue')
	CheckPointData.Name = 'CheckPoint'
	CheckPointData.Parent = PlayerStats
	if CheckPointData.Value == nil then
		CheckPointData.Value = workspace.Checkpoints.One
	end
		end
	end)
end)


game.Players.PlayerRemoving:Connect(function(p)
	ServerStorage:WaitForChild('CheckPointFolder'):WaitForChild(p.UserId):Destroy()
end)

StarterCharacterScripts

ServerStorage = game:GetService("ServerStorage")
Storage = ServerStorage.CheckPointFolder

script.Parent:WaitForChild('Humanoid').Touched:Connect(function(Hit)
	if Hit:IsA('SpawnLocation') then
		if script.Parent:WaitForChild('Humanoid').Health > 0 then
		local Player = game.Players:GetPlayerFromCharacter(script.Parent)
		Storage:WaitForChild(Player.UserId).CheckPoint.Value = Hit
		Player.RespawnLocation = Storage:WaitForChild(Player.UserId).CheckPoint.Value
			else
		end
	end
end)

Any clue why its not working as intended

1 answer

###Instead of saving them in ServerStorage, I'd recommend saving them with DataStoreService. You will save the name of the checkpoint, assigning to One if they are new to the game. I will create a StringValue in the Player with a Value of their checkpoint.

local ServerStorage = game:GetService("ServerStorage") -- use more local variables
local DataStoreService = game:GetService("DataStoreService") -- service to use
local CheckpointData = DataStoreService:GetDataStore("CheckpointData") -- your data store 
local key -- the key we will use 

game.Players.PlayerAdded:Connect(function(p)
    key = "Checkpoints_"..p.UserId -- example key 
    
    local checkpoint = Instance.new("StringValue")
    checkpoint.Name = "Checkpoint"
    checkpoint.Value = CheckpointData:GetAsync(key) or "One"
    -- if we have data, load it. else, default of "One"
    checkpoint.Parent = p
    
    p.CharacterAdded:Connect(function(c)
        local data = CheckpointData:GetAsync(key)
        
        if data then -- if there is data 
            -- Load spawn 
            c:SetPrimaryPartCFrame(workspace.Checkpoints[data].CFrame)
        else 
            -- first time playing, load them first checkpoint
            c:SetPrimaryPartCFrame(workspace.Checkpoints.One.CFrame)
        end
    end)
end)


game.Players.PlayerRemoving:Connect(function(p)
    CheckpointData:SetAsync(key, p.Checkpoint.Value) -- save
end)

###The script in StarterCharacterScripts is useless lol

5 comments

0

I didn't plan for it to save, but I could use this

0

oh. but you had a PlayerRemoving event so I assumed you wanted it to save but ok

0

makes sense to misunderstand, but I could make it save by not having setAsync to save data.

0

probably

0

yeh but then you'd have to get rid of the GetAsync and stuff.

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