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

Is there an Alternative way to do this?

i'm Scripting a Filtering Enabled Teleport Tool, you may have seen in some games wich contain a Teleport Tool, right? the problem is that in some of those games you can teleport to.. the void.. but, with my Teleport tool you can only teleport within a range of 200 Studs, heres my code:

Local Script:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait(.1)
local Tool = script.Parent
local Mouse = Player:GetMouse()
local CanTeleport = true

Tool.Activated:Connect(function()
	if CanTeleport then
		CanTeleport = false
		Tool.TeleportEvent:FireServer(Mouse.Hit.p, Mouse.Target,
Character:FindFirstChild("HumanoidRootPart"))
		wait(1)
		CanTeleport = true
	end
end)

Server Script:

local Tool = script.Parent
local Range = 200

function CanTeleport(Position, TargetPart, RootPart)
	if Position ~= nil and TargetPart ~= nil and RootPart ~= nil then
		if Position.X < RootPart.Position.X + Range and Position.Y < RootPart.Position.Y + Range and Position.Z < RootPart.Position.Z + Range then
			if Position.X > RootPart.Position.X - Range and Position.Y > RootPart.Position.Y - Range and Position.Z > RootPart.Position.Z - Range then
				return true
			end
		end
	end
end

Tool.TeleportEvent.OnServerEvent:Connect(function(Client, MousePosition, MouseTarget, RootPart)
	if CanTeleport(MousePosition, MouseTarget, RootPart) then
		print('teleported')
	end
end)

You see CanTeleport() function is very complex, i want to know if i can use an alternative way so i don't have to make complex calculus, i got dizzy while trying to make those calculus @-@

1 answer

✓ Accepted answer

Instead of checking each variable you can use a vector subtraction and magnitude like this:

if (Position - TargetPosition).magnitude <= 200 then
--teleport
else
--don't teleport
end

As a note, this works because it gets the different of each variable output = Vector3.new(5,4,3)-Vector3.new(4,3,1) --This would give us Vector3.new(1,1,2) and taking the magnitude of that (basically doing the pythagorean theorem in 3D) will give us the total distance.

2 comments

0

now my code look much Cleaner :)

0

Glad I could help :)

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