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

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

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

9 comments

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?

0

Probably, but I really want to get this fixed sense I kinda just started to learn how to use magnitude now.

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.

0

How though?

0

Exactly. I suppose you could read more about it on the wiki... Tutorial videos are good.

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.

0

No tutorials that I found on magnitude showed how to set up more then one magnitude in a script.

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

1 answer

✓ Accepted answer

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

3 comments

0

Thanks. (you didn't miss anything btw ; )"

0

That's more like it!

0

^

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