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

I need help fixing my obby checkpoints if U can help please do?

For the past months I been trying to fix a bug, where in my obby I play with certain players and each time I die it spawns me at the lobby then it takes like 5-10 seconds to respawn back to the checkpoint I am really Here is a small video of what I mean https://youtu.be/iLGI_TOoM6I Anybody who can fix the checkpoints it prevents me to have growth of players in this obby because of this bug please help me... I been trying fix it for months legit here is the script of the checkpoints they all have the same script;


local spawn = script.Parent
spawn.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local checkpointData = game.ServerStorage:FindFirstChild("CheckpointData")
		if not checkpointData then
			checkpointData = Instance.new("Model", game.ServerStorage)
			checkpointData.Name = "CheckpointData"
		end
		
		local checkpoint = checkpointData:FindFirstChild(tostring(player.userId))
		if not checkpoint then
			checkpoint = Instance.new("ObjectValue", checkpointData)
			checkpoint.Name = tostring(player.userId)
			
			player.CharacterAdded:connect(function(character)
				wait()
				character:WaitForChild("HumanoidRootPart").CFrame = game.ServerStorage.CheckpointData[tostring(player.userId)].Value.CFrame + Vector3.new(0, 4, 0)
			end)
		end
		
		checkpoint.Value = spawn
	end
end)

1 answer

GENERAL PRACTICE

Use the :GetService() method to check for the "ServerStorage" and "Players" rather than immediately calling them.

You don't require the tostring() on the UserId since the numbers can be directly taken by the script.

Considering how this code is written, it's probably better to separate the .Touched and .CharacterAdded events since you will have the character added before touching a part.

ISSUES

connect is deprecated in favor of Connect

The actual Property name for a player's id is UserId, not "userId"

Use the :SetPrimaryPartCFrame() of models (the character is a model) to move the player since just moving their rootpart may sometimes cause the player's body to remain where it is and only move the one part.

spawn is a function, so consider changing the name of this variable (my example switches it to "sp")

REVISED SERVER SCRIPT

game:GetService("Players").PlayerAdded:Connect(function(player)
	local objval = Instance.new("ObjectValue")
	objval.Name = "CheckpointData"
	objval.Value = nil
	objval.Parent = player
	
	player.CharacterAdded:Connect(function(character)
		repeat wait() until character:FindFirstChild("HumanoidRootPart") ~= nil
		if objval.Value ~= nil then
			character:SetPrimaryPartCFrame(objval.Value.CFrame + Vector3.new(0, 4, 0))
		end
	end)
end)

for w, sp in pairs(workspace:WaitForChild("Checkpoints"):GetChildren()) do
	sp.Touched:Connect(function(hit)
		if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
			local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			local checkpointData = player:WaitForChild("CheckpointData")
			if checkpointData.Value ~= sp then
				checkpointData.Value = sp
			end
		end
	end)
end

2 comments

0

Can you give me steps what to do how to use this REVISED script because I read what you typed and I understood why but for some reason I dont know how to put it correctly, however When I used this REVISED script I tried putting it on many ways of u said like server storage etc, can you give steps to what to do when im in the studio because every object of a checkpoint has the same script inside

0

I think you have to put it in the ServerScriptService or in the Checkpoint blocks/parts

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