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

Why isnt magnitude working correctly?

Asked by
DevingDev 346 Moderation Voter
8 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

So, if your distance is not 10 then the billboard gui will be enabled but if you are in 10 distance then the gui will get removed but for some dumb reason it dosent do that so insted it are giving me an error why?

Script.

local door = game.workspace.Gandalf.head

while true do
    for i,v in pairs(game.Players:GetChildren()) do
        if (v.Character.Torso.Position - door.Position).magnitude <= 10 then
            if door.TrackGui then
                door.TrackGui:Destroy()
            end
        end
    end

    for i,v in pairs(game.Players:GetChildren()) do
        if (v.Character.Torso.Position - door.Position).magnitude >= 10 then
            game.ServerStorage.TrackGui:Clone().Parent = door
        end
    end
wait()
end

Error.

23:39:19.341 - TrackGui is not a valid member of Part
23:39:19.342 - Stack Begin
23:39:19.342 - Script 'Workspace.Gandalf.Head.Script', Line 6

1 answer

Log in to vote
1
Answered by
CodeNil 30
8 years ago

You need to use FindFirstChild, as line 6 checks if trackgui exists, but its not checking, its assuming it already exists.

if door:FindFirstChild("TrackGui") then

2 other mistakes, when creating the new billboard, you dont check if one already exists. Also, don't you need to set adornee of a billboard?

Anyway, your code is very very very inefficient. Your doing the loop on wait(), that is way too fast and is not needed. Also, creating and destroying objects is very inefficient, rather create the billboard and change the Enabled property of the billboardgui

local door = workspace.Gandalf.head

local Billboard = game.ServerStorage.TrackGui:Clone()
Billboard.Parent = door
Billboard.Adornee = door

while wait(0.5) do
    local InView = false
    for _, v in pairs(game.Players:GetChildren()) do
        if (v.Character.Torso.Position - door.Position).magnitude <= 10 then
            InView = true
        end
    end
    Billboard.Enabled = InView
end
Ad

Answer this question