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

Checkpoint player teleportation not working?

Hello, greeting to you and thanks for coming. What I am doing is to script a checkpoint. The script did not work, when I run it, it does not output any errors, but just not working. I checked the reason by using a print(“success”) function and found out it is the CFrame part’s error, but I do not know where is the mistake, anyone can point it out for me?

Here is my script:

game.Players.PlayerAdded:connect(function(player)
	spawn = false
	script.Parent.Touched:connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name == player.Name then
			spawn = true
		end
	end)
	player.CharacterAdded:connect(function(respawn)
		if spawn then
			player.Character.HumanoidRootPart.CFrame = CFrame.new(script.Parent.Position.X,script.Parent.Position.Y+5,script.Parent.Position.Z)
-- It is the problem of here.
		end
	end)
end)

My script is a normal script, it is not a localscript or modulescript.

If you know where is the mistake then please tell me, thanks.

1 answer

✓ Accepted answer
-- Pretty simple to do. However you had a whole wrong approach to it.
--Insert all of the below code into one script, and place it anywhere.

game.Players.PlayerAdded:Connect(function(player) -- When any player is added to the server, run below code for that player.
	local checkpoint = Instance.new("ObjectValue") -- Create an object value that will define which checkpoint the player is at.
	checkpoint.Name = "Checkpoint" -- Anytime you call instance.new, the part created will be named whatever you created, in this case its default name is "ObjectValue", so lets change that to something we can use.
	checkpoint.Parent = player -- Set the objectvalues parent to player so we can access it later.
	player.CharacterAdded:Connect(function(character) -- Whenever the player's character is loaded in, run below code. Will run anytime the player's character resets.
		repeat wait() until character:FindFirstChild("HumanoidRootPart") -- Wait until the humanoidrootpart is actually made before teleporting
		if player.Checkpoint.Value ~= nil then -- If the previous objectvalues actual value isn't nil, run below code. Otherwise it will spawn them wherever your have a spawnpoint
			character.HumanoidRootPart.CFrame = player.Checkpoint.Value.CFrame * CFrame.new(0,3,0) -- Set players cframe to their current objectvalues cframe, plus some distance so he doesnt get stuck.
		end
	end)
end)

--Now when it comes to detecting if a player touches the checkpoint, its better to have the code seperate from the player code.
-- For simplicity we will place all the checkpoints in one folder inside workspace, and use a loop on them to detect player touches.
for _,Checkpoint in pairs(workspace.Checkpoints:GetChildren()) do -- Iterate through all of the checkpoints, and run the below code on each one.
	Checkpoint.Touched:Connect(function(hit)
		if game.Players:FindFirstChild(hit.Parent.Name) then
			game.Players:FindFirstChild(hit.Parent.Name).Checkpoint.Value = Checkpoint -- If touched part is detected as a player, set their checkpoint to the touched checkpoint
		end
		if game.Players:FindFirstChild(hit.Parent.Parent.Name) then
			game.Players:FindFirstChild(hit.Parent.Parent.Name).Checkpoint.Value = Checkpoint
		end
	end)
end

-- Using this script will allow you to use as many checkpoints as you want, just place them wherever you want on the map, and parent them to workspace.Checkpoints

Lets break down why your code doesn't work

game.Players.PlayerAdded:connect(function(player) -- First of all :connect is depreciated. It still works but hasnt been updated/modified in a long time. Use :Connect instead.
    spawn = false -- You shouldn't be defining something without using local, such as local spawn = false. You are trying to edit something that doesnt exist.
    script.Parent.Touched:connect(function(hit) -- This shouldnt be called here. Now for every player that joins, this checkpoint will try to run this function. So with 8 players, when a player touches it the script will run 8 times.
        if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name == player.Name then
            spawn = true
        end
    end)
    player.CharacterAdded:connect(function(respawn) -- where you have respawn is actually the character.
        if spawn then -- you are just checking if spawn exists, not if its true or false.
            player.Character.HumanoidRootPart.CFrame = CFrame.new(script.Parent.Position.X,script.Parent.Position.Y+5,script.Parent.Position.Z)
-- It is the problem of here. -- This should work, but isn't the best way. Overall the rest of your script just breaks this line.
        end
    end)
end)

7 comments

0

Yeah, but, can you just please tell me why is my script not working and not returning any errors?

0

I do understand why is your code working, what I am wondering is why is my script not working.

0

I edited my answer to explain why yours doesnt.

0

thx

0

Hi, I am still a bit confused, because even though I changed spawn into spawn == true and locales the spawn it still does not work.

0

hello?

0

Your code itself is not going to work as I stated. You will have to recode the entire thing, which I did for you. Compare my script and your script and see where you can make improvements in future code.

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