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

How do i make it so the user can only teleport for a maximum of 100 studs?


local p=game.Players.LocalPlayer
    local char=p.Character
 local root=char:WaitForChild('HumanoidRootPart')
local mouse=p:GetMouse()

function tele()
char.Humanoid:MoveTo(mouse.Hit.p)
end

mouse.Button1Down:Connect(function()
	
tele()

end)

the script above would allow the player to teleport anywhere on the map without limitations. is there any way for it to not activate if the distance is farther than 100 studs?

2 comments

0

Humanoid MoveTo makes the character walk to a location, not teleport.

0

why don't you just use mouse.Button1Down(tele)

1 answer

Check the distance with an if statement. You also have other important problems.

--consider using variable "player" over "p"
local p=game.Players.LocalPlayer
local char=p.Character or p.CharacterAdded:wait() --incase char is not loaded
local root=char:WaitForChild('HumanoidRootPart')
local mouse=p:GetMouse()

function tele()
	--define vectors
	local origin = root.CFrame.p
	local target = mouse.Hit.p + Vector3.new(0,3,0) --elevate root
	if (target-origin).magnitude < 100 then --distance between
		--MoveTo should be used on character, not humanoid
		char:MoveTo(target)
	end
end

--since tele is a basic function, you can simply pass it in
mouse.Button1Down:Connect(tele)

If you don't want to prohibit the teleport, but minimize it, you can shorten the position vector.

function tele()
	local origin = root.CFrame.p
	local target = mouse.Hit.p + Vector3.new(0,3,0)
	local diff = target-origin
	if diff.magnitude > 100 then
		--same direction (unit) but length 100
		target = origin + diff.unit * 100
	end
	char:MoveTo(target)
end

Edit: It's best to offset the mouse height.

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