E to teleport problems

problem: when i die and then when my character is added and i press e it says
"HumanoidRootPart is not a valid member of Model “Workspace.butter5432m”
script:

--getting all these stuff
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait(1)
local debounce = false

local mouse = plr:GetMouse()

local pos = mouse.Hit.Position

pos = CFrame.new(pos.X,pos.Y,pos.Z)

local UIS = game:GetService("UserInputService")



local Frame = plr.PlayerGui.ScreenGui.Frame






--when uis begins
UIS.InputBegan:Connect(function(input)
	
	if input.KeyCode == Enum.KeyCode.E then
		if debounce then return end
		
		debounce = true

		Frame.BackgroundColor3 = Color3.new(1, 0, 0.0156863)

		local mouse = plr:GetMouse()
		
		local pos = mouse.Hit.Position
		
		pos = CFrame.new(pos.X,pos.Y,pos.Z)
		
		char.HumanoidRootPart.CFrame = pos 
		
		task.wait(3)

		debounce = false

		Frame.BackgroundColor3 = Color3.new(0, 255, 0)
		
		
	end
end)

Also, how do i add a max distance like if i press e at the edge of the map it wont teleport me but if i do it like this

nop

In the picture above, you will see 2 rectangles and a circle
the rectangles are the baseplate and the circle is the max you can go
since its mouse .hit it will go inf distance which i dont want

local character = player.Character

if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end

local HRP = character:WaitForChild("HumanoidRootPart")

This should work fine, just remember to change the variables in the rest of the script.

1 Like

what does the line
“if not character or not character.Parent then” mean?

or the short version

local character = player.Character or player.CharacterAdded:Wait()
local HRP = character:WaitForChild("HumanoidRootPart")
local userInput = game:GetService("UserInputService")

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()

userInput.InputBegan:Connect(function(input, processed)
	if processed then return end

	if input.KeyCode == Enum.KeyCode.E then
		character:PivotTo(mouse.Hit)
	end
end)

This is a shortened version of your script which is working for me (placed inside StarterCharacterScripts).

1 Like

I think it is checking if character exists, if not it waits for the player to be respawned.

and for the distance,

you can do

local distance = (position1 - position2).magnitude

and use an if statement to check the distance

1 Like

it means that if the character doesnt exist or if the characters parent (the player) doesnt exist then it’ll wait for it to be loaded and set the character variable to the new loaded one

1 Like