Obby Teleporter

I want to teleport the player in another area of the game(not different place), but I receive an error.
Error: attempt to index nil with ‘Character’
Script:

local plr = game.Players.LocalPlayer

game.ReplicatedStorage.Obby.Start.EasyStart.OnClientEvent:Connect(function(plr)
	plr.Character.CFrame = script.Parent.Parent.Parent.Parts.Portal.CFrame
	plr.Values.EasyObby.Value.Value = 180
	local portal = game.Workspace.Obby.Easy.Portal.Portal
	portal.BrickColor = BrickColor.new("Black")
	portal.CanCollide = true
	portal.Transparency = 0
	while plr.Values.EasyObby.Value.Value do
		wait(1)
		if plr.Values.EasyObby.Value.Value > 0 then
			plr.Values.EasyObby.Value.Value -= 1
		end
	portal.BrickColor = BrickColor.new("Lime green")
	portal.CanCollide = true
	portal.Transparency = 0.5
	end
end)

If I make this work, I will be able to make the end teleport! Thanks for helping!

5 Likes

The player spawn befor character load

can you show an example of the code

	while true do wait()
		if plr.Character then break end
	end
1 Like

Place it befor you call character


game.ReplicatedStorage.Obby.Start.EasyStart.OnClientEvent:Connect(function(plr)
while true do wait()
		if plr.Character then break end
	end
	plr.Character.CFrame = script.Parent.Parent.Parent.Parts.Portal.CFrame
	plr.Values.EasyObby.Value.Value = 180
	local portal = game.Workspace.Obby.Easy.Portal.Portal
	portal.BrickColor = BrickColor.new("Black")
	portal.CanCollide = true
	portal.Transparency = 0
	while plr.Values.EasyObby.Value.Value do
		wait(1)
		if plr.Values.EasyObby.Value.Value > 0 then
			plr.Values.EasyObby.Value.Value -= 1
		end
	portal.BrickColor = BrickColor.new("Lime green")
	portal.CanCollide = true
	portal.Transparency = 0.5
	end
end)
2 Likes

still have the error on line 7 “attempt to index nil with ‘Character’”

1 Like

Update: I tried to do everything I could do but I’m trapped in the “attempt to index nil” problem. I got “attempt to index nil with values” and then “waitForChild”.

My script: (no problem here)

script.Parent.Touched:Connect(function(hit)
	if hit then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr then
			local cooldown = plr.Values.EasyObby
			local humanoidrootpart = plr.Character:FindFirstChild("HumanoidRootPart")
			if humanoidrootpart then
				if cooldown.Value == 0 then
					game.ReplicatedStorage.Obby.Start.EasyStart:FireClient(plr)
					plr.Character.CFrame = script.Parent.Parent.Parent.Parts.Portal.CFrame
					print("Teleporting...")
				end
			end
		end
	end
end)

My local script in starterPlayerScripts:

local plr = game:GetService("Players").LocalPlayer

-- Easy Mode

game.ReplicatedStorage.Obby.Start.EasyStart.OnClientEvent:Connect(function(plr)
	plr:WaitForChild("Values").EasyObby.Value = 180
	local portal = game.Workspace.Obby.Easy.Portal.Portal
	portal.BrickColor = BrickColor.new("Black")
	portal.CanCollide = true
	portal.Transparency = 0
	while plr.Values.EasyObby.Value do
		wait(1)
		if plr.Values.EasyObby.Value > 0 then
			plr.Values.EasyObby.Value -= 1
		end
		portal.BrickColor = BrickColor.new("Lime green")
		portal.CanCollide = true
		portal.Transparency = 0.5
	end
end)

remove plr in the function’s connection.

i tried that and it did not work.

why???

how does that benefit this code at all

You should define a Character variable this way:

local Character = plr.Character or plr.CharacterAdded:Wait()

try this

local plr = game.Players.LocalPlayer

game.ReplicatedStorage.Obby.Start.EasyStart.OnClientEvent:Connect(function(plr)
    local character = plr.Character or plr.CharacterAdded:Wait()
	character.HumanoidRootPart.CFrame = script.Parent.Parent.Parent.Parts.Portal.CFrame
	plr.Values.EasyObby.Value.Value = 180
	local portal = game.Workspace.Obby.Easy.Portal.Portal
	portal.BrickColor = BrickColor.new("Black")
	portal.CanCollide = true
	portal.Transparency = 0
	while plr.Values.EasyObby.Value.Value do
		wait(1)
		if plr.Values.EasyObby.Value.Value > 0 then
			plr.Values.EasyObby.Value.Value -= 1
		end
	portal.BrickColor = BrickColor.new("Lime green")
	portal.CanCollide = true
	portal.Transparency = 0.5
	end
end)

If this doesn’t work, remove “plr” in the function().

doesnt work, i tried it and tried it when i removed “plr” in the function().

what is the specific error that you are getting from the code?

The error is right there… Remove plr from game.ReplicatedStorage.Obby.Start.EasyStart.OnClientEvent:Connect(function(plr) or either change local plr to local player.

Or use this;

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

game.ReplicatedStorage.Obby.Start.EasyStart.OnClientEvent:Connect(function()
	if Character then Character.CFrame = script.Parent.Parent.Parent.Parts.Portal.CFrame end
	
	Player.Values.EasyObby.Value.Value = 180
	
	local portal = game.Workspace.Obby.Easy.Portal.Portal
	portal.BrickColor = BrickColor.new("Black")
	portal.CanCollide = true
	portal.Transparency = 0
	
	while Player.Values.EasyObby.Value.Value do
		task.wait(1)
		
		if Player.Values.EasyObby.Value.Value > 0 then
			Player.Values.EasyObby.Value.Value -= 1
		end
		
		portal.BrickColor = BrickColor.new("Lime green")
		portal.CanCollide = true
		portal.Transparency = 0.5
	end
end)

You should not use a LocalScript to teleport a player’s character positionally because then it won’t replicate to other clients.

You should use a Server script.

Instead, you should just teleport a player’s character model using :PivotTo( the part’s cframe here ) whenever the player starts an obby whether through touching the part or joining the game.

isn’t the player character owned by the client?

It is, but only on their side, but other clients won’t see it.