Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

Teleport tool with limited distance not working. Why?

Asked by
OnaKat 446 Moderation Voter
2 months ago

I'm making a teleport tool that has a limited distance, so players can't teleport too long range. I don't know how to do it.

I try with this local script.

local tool = script.Parent.Parent
tool.Activated:Connect(function()
    local Torso = script.Parent.Parent.Torso
    local mouse = game.Players.LocalPlayer:GetMouse()
    for x = Torso.Position.X-31,Torso.Position.X+31,0.000000000001 do
        for z = Torso.Position.Y-31,Torso.Position.Y+31,0.000000000001 do
            wait()
            if mouse.Hit.p == Vector3.new(x,mouse.Hit.p.Y,z) then
                script.Parent.Parent:moveTo(mouse.Hit.p)
            end
        end
    end
end)

But it didn't work. Please help!

0
Try script.Parent.Parent:SetPrimaryPartCFrame(mouse.Hit.p) Tweakified 111 — 1mo

1 answer

Log in to vote
0
Answered by 2 months ago

The reason why it wasn't working is because of this line of code:

script.Parent.Parent:moveTo(mouse.Hit.p)

In the first line of your local script, it reads that Tool is defined as script.Parent.Parent. You did not set the Character, but rather, the tool. This is a very easy fix.

local tool = script.Parent.Parent
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
tool.Activated:Connect(function()
    local Torso = script.Parent.Parent.Torso
    local mouse = game.Players.LocalPlayer:GetMouse()
    for x = Torso.Position.X-31,Torso.Position.X+31,0.000000000001 do
        for z = Torso.Position.Y-31,Torso.Position.Y+31,0.000000000001 do
            wait()
            if mouse.Hit.p == Vector3.new(x,mouse.Hit.p.Y,z) then
                Character:MoveTo(mouse.Hit.p)
            end
        end
    end
end)

We made a few changes in the code. We basically created two variables, one for the player and one for the Character. We use the variable "Player" to help define the "Character" variable by either getting the Character or waiting for the Character to spawn in case it hasn't yet. In the event in which the Tool is activated we do some conditional statements created by you and we move the Character. I also changed moveTo() into MoveTo() since moveTo() is a non-existent function.

That's it, and if something is not right make sure to double check your code and debug any errors.

>

0
Sorry. It's "local tool = script.Parent" But it still not working :( OnaKat 446 — 2mo
Ad

Answer this question