Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Magnitude needs to be fixed?

Asked by 10 years ago

why wont this work?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local magnitude = (player.Character.Torso.Position - workspace.Door.Root.Position).magnitude
p = 0




while wait() do
    if magnitude < 20 then
        p = 1
        scrn = Instance.new("ScreenGui",player.PlayerGui)
        txt = Instance.new("TextLabel",scrn)
        txt.BackgroundTransparency = 1
        txt.Text = "Click to open door."
        txt.Position = UDim2.new(0, 600,0, 500)
        txt.Size = UDim2.new(0, 500, 0, 50)
        txt.TextScaled = true
        txt.TextColor3 = BrickColor.new("White").Color



    end
end

1 answer

Log in to vote
3
Answered by 10 years ago
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
p = 0

function getDistance()
    return player.Character and player:DistanceFromCharacter(workspace.Door.Root.Position) or math.huge
end


while wait() do
    if getDistance() < 20 then
        p = 1
        scrn = Instance.new("ScreenGui",player.PlayerGui)
        txt = Instance.new("TextLabel",scrn)
        txt.BackgroundTransparency = 1
        txt.Text = "Click to open door."
        txt.Position = UDim2.new(0, 600,0, 500)
        txt.Size = UDim2.new(0, 500, 0, 50)
        txt.TextScaled = true
        txt.TextColor3 = BrickColor.new("White").Color



    end
end

You were not updating the magnitude variable, so you were always checking the distance of the character's spawning location to the door's position. DistanceFromCharacter is also more efficient.

0
Thanks for the help :) jbjgang2 37 — 10y
Ad

Answer this question