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

Magnitude based touch pad?

local breakchecks = false
local pads = {script.Parent.p1, script.Parent.p2, script.Parent.p3, script.Parent.p4}
for i, v in pairs(pads) do
	spawn(function()
	while wait() do
			for i, plr in pairs(game.Players:GetPlayers()) do
				if plr.Character then
					local mag = (v.Position - plr.Character:WaitForChild("Torso").Position).magnitude
					if mag < 4 then
						print("near")
							v.BrickColor = BrickColor.new("Baby blue")
					end
					repeat wait() until mag >= 4
					v.BrickColor = BrickColor.new("Persimmon")
				end
			end
		end
	end)
end

I assumed that repeat wait() would continue until the player walked away, but "near" only prints once and the color doesn't change once it is set to "Baby blue."

1 comment

1

The magnitude is calculated as a const; you're using a static number, you need to recalculate their distance each time.

1 answer

✓ Accepted answer

Feahren is correct. "The magnitude is calculated as a const; you're using a static number, you need to recalculate their distance each time.


Edited code:

local breakchecks = false
local pads = {script.Parent.p1, script.Parent.p2, script.Parent.p3, script.Parent.p4}
for i, v in pairs(pads) do
    spawn(
        function()
            while wait() do
                for i, plr in pairs(game.Players:GetPlayers()) do
                    if plr.Character then
                        if (v.Position - plr.Character:WaitForChild("Torso").Position).magnitude < 4 then
                            print("near")
                            v.BrickColor = BrickColor.new("Baby blue")
                        end
                        repeat
                            wait()
                        until (v.Position - plr.Character:WaitForChild("Torso").Position).magnitude >= 4
                        v.BrickColor = BrickColor.new("Persimmon")
                    end
                end
            end
        end
    )
end

1 comment

0

Obvious oversight for me, thank you.

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