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

Making a detection script using magnitude but it doesn't work?

Asked by 5 years ago

My goal here was to have the text keep on changing as you get closer to the part but only this part of the code works.

repeat wait() until game.Players.LocalPlayer.Character

while wait() do
            for i,v in pairs(game.Players:GetChildren()) do
                    if (v.Character.Head.Position - game.Workspace.DEF.Torso.Position).magnitude <= 160 then
                            script.Parent.Frame.detect.Text = "Tiny amounts of Dark Energy Detected, be aware"
                    else
                            script.Parent.Frame.detect.Text = "No Dark Energy Detected"
        end
    end
end

The rest of the code doesn't work, what am I doing wrong here?

repeat wait() until game.Players.LocalPlayer.Character

while wait() do
            for i,v in pairs(game.Players:GetChildren()) do
                    if (v.Character.Head.Position - game.Workspace.DEF.Torso.Position).magnitude <= 160 then
                            script.Parent.Frame.detect.Text = "Tiny amounts of Dark Energy Detected, be aware"
                    else
                            script.Parent.Frame.detect.Text = "No Dark Energy Detected"
        end
    end
end

while wait() do
            for i,v in pairs(game.Players:GetChildren()) do
                    if (v.Character.Head.Position - game.Workspace.DEF.Torso.Position).magnitude <= 140 then
                            script.Parent.Frame.detect.Text = "Low amounts of Dark Energy Detected, advised leaving the area"
                    else
                            script.Parent.Frame.detect.Text = "Tiny amounts of Dark Energy Detected, be aware"
        end
    end
end

while wait() do
            for i,v in pairs(game.Players:GetChildren()) do
                    if (v.Character.Head.Position - game.Workspace.DEF.Torso.Position).magnitude <= 120 then
                            script.Parent.Frame.detect.Text = "Dark Energy Detected, Leave area"
                    else
                            script.Parent.Frame.detect.Text = "Low amounts of Dark Energy Detected, advised leaving the area"
        end
    end
end

while wait() do
            for i,v in pairs(game.Players:GetChildren()) do
                    if (v.Character.Head.Position - game.Workspace.DEF.Torso.Position).magnitude <= 100 then
                            script.Parent.Frame.detect.Text = "Lots of Dark Energy Detected, Leave area!"
                    else
                            script.Parent.Frame.detect.Text = "Dark Energy Detected, Leave area"
        end
    end
end

while wait() do
            for i,v in pairs(game.Players:GetChildren()) do
                    if (v.Character.Head.Position - game.Workspace.DEF.Torso.Position).magnitude <= 80 then
                            script.Parent.Frame.detect.Text = "Large amounts of Dark Energy Detected, Leave area now!"
                    else
                            script.Parent.Frame.detect.Text = "Lots of Dark Energy Detected, Leave area!"
        end
    end
end
repeat wait() until game.Players.LocalPlayer.Character

while wait() do
            for i,v in pairs(game.Players:GetChildren()) do
                    if (v.Character.Head.Position - game.Workspace.DEF.Torso.Position).magnitude <= 160 then
                            script.Parent.Frame.detect.Text = "Tiny amounts of Dark Energy Detected, be aware"
                    else
                            script.Parent.Frame.detect.Text = "No Dark Energy Detected"
        end
    end
end

while wait() do
            for i,v in pairs(game.Players:GetChildren()) do
                    if (v.Character.Head.Position - game.Workspace.DEF.Torso.Position).magnitude <= 140 then
                            script.Parent.Frame.detect.Text = "Low amounts of Dark Energy Detected, advised leaving the area"
                    else
                            script.Parent.Frame.detect.Text = "Tiny amounts of Dark Energy Detected, be aware"
        end
    end
end

while wait() do
            for i,v in pairs(game.Players:GetChildren()) do
                    if (v.Character.Head.Position - game.Workspace.DEF.Torso.Position).magnitude <= 120 then
                            script.Parent.Frame.detect.Text = "Dark Energy Detected, Leave area"
                    else
                            script.Parent.Frame.detect.Text = "Low amounts of Dark Energy Detected, advised leaving the area"
        end
    end
end

while wait() do
            for i,v in pairs(game.Players:GetChildren()) do
                    if (v.Character.Head.Position - game.Workspace.DEF.Torso.Position).magnitude <= 100 then
                            script.Parent.Frame.detect.Text = "Lots of Dark Energy Detected, Leave area!"
                    else
                            script.Parent.Frame.detect.Text = "Dark Energy Detected, Leave area"
        end
    end
end

while wait() do
            for i,v in pairs(game.Players:GetChildren()) do
                    if (v.Character.Head.Position - game.Workspace.DEF.Torso.Position).magnitude <= 80 then
                            script.Parent.Frame.detect.Text = "Large amounts of Dark Energy Detected, Leave area now!"
                    else
                            script.Parent.Frame.detect.Text = "Lots of Dark Energy Detected, Leave area!"
        end
    end
end
0
Not trying to come out as rude, but, isn't that a little overkill? What do you think? (Incoming joke): you're not trying to create Frankenstein - are you? DaggerOf_Fly -24 — 5y
0
Probably, but I really want to get this fixed sense I kinda just started to learn how to use magnitude now. LoganboyInCO 150 — 5y
0
Well you started head-dive deep. Try using magnitude in other (smaller) scenarios. It will help you to continue your project, once you're more familiar with it. DaggerOf_Fly -24 — 5y
0
How though? LoganboyInCO 150 — 5y
View all comments (5 more)
0
Exactly. I suppose you could read more about it on the wiki... Tutorial videos are good. DaggerOf_Fly -24 — 5y
0
Maybe someone with a little more free-time could help you, but it's (I would say 'above my paygrade') but I don't get paid. I'll leave it at that. DaggerOf_Fly -24 — 5y
0
No tutorials that I found on magnitude showed how to set up more then one magnitude in a script. LoganboyInCO 150 — 5y
0
The while loops are infinite, they don't break. In your case at least. The first loop needs to break for the second one to execute, and so on and so forth. I don't think you need so many loops but you could wrap each loop in a spawn User#19524 175 — 5y

1 answer

Log in to vote
2
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

Your problem is having a bunch of while loops that you want to run simultaneously to check if a player is getting nearer and nearer. Know that a while loop keeps running until you "break" out of it or it's conditional is set false.

You could use spawn() function but the problem with this is you'll have multiple loops still running and it'll just look messy. You can condense the code into a single loop but will have to use a number of if-statements. I know there's fancier ways of doing this but to address the main issue:

local def = workspace.DEF

while true do
    for i, v in pairs(game.Players:GetPlayers()) do
        if v.Character then
            local hrp = v.Character.HumanoidRootPart
            local   distance = (hrp.Position - def.HumanoidRootPart.Position).magnitude

            if distance <= 10 then
                --do stuff
            elseif distance > 10 and distance <= 20 then
                --do stuff
            elseif distance > 20 and distance <= 30 then
                --do stuff
            end         
        end
    end
    wait()
end

Please comment if I missed something. In a class rn qq

0
Thanks. (you didn't miss anything btw ; )" LoganboyInCO 150 — 5y
0
That's more like it! DaggerOf_Fly -24 — 5y
0
^ LoganboyInCO 150 — 5y
Ad

Answer this question