hello I would like to make a loop that checks if a player is within a certain radius
the script itself works but when I put a music on, it repeats itself every two seconds because of the loop
so do you have an idea to put something else in place of "while true do"
script :
local Torso = script.Parent.Parent.Torso sonic = script.Parent.Parent.Sonic torsop = script.Parent.Parent.Torsoe head = script.Parent.Parent.Heade fly = script.Parent.Parent.SonicFly lefta = script.Parent.Parent["Left Arm"] righta = script.Parent.Parent["Right Arm"] leftl = script.Parent.Parent["Left Leg"] rightl = script.Parent.Parent["Right Leg"] eye1 = script.Parent.Parent.Eyes1 eye2 = script.Parent.Parent.Eyes2 Chase = script.Parent.Chase ----------------------------------- BASIC ATTACK ------------------------------- -------------------------- Sonic kills and chase ------------------------------- while true do for i,v in pairs(game.Players:GetChildren()) do if (v.Character.Torso.Position - Torso.Position).magnitude <= 80 then -- pos change torsop.Transparency = 1 head.Transparency = 1 righta.Transparency = 1 lefta.Transparency = 1 leftl.Transparency = 1 rightl.Transparency = 1 fly.Transparency = 0 eye1.Transparency = 1 eye2.Transparency = 0 -- char change sonic.WalkSpeed = 30 Chase:Play() else -- pos change torsop.Transparency = 0 head.Transparency = 0 righta.Transparency = 0 lefta.Transparency = 0 leftl.Transparency = 0 rightl.Transparency = 0 fly.Transparency = 1 eye1.Transparency = 0 eye2.Transparency = 1 -- char change sonic.WalkSpeed = 18 end end wait(2) end
thanks for helping
EDIT : I also want to make sure that the sound is played for one person
You're iterating all players so you'll need to break the loop when it finds someone to chase, and keep track if it couldn't find anyone. You also need to store if it's active or not so you can check that before activating it again if it's already active. Something like this perhaps:
local active = false -- currently in chase? local found = false -- anyone within range? while wait(2) do found = false -- reset before checking again for i,v in pairs(game.Players:GetChildren()) do local range = (v.Character.HumanoidRootPart.Position - Torso.Position).magnitude -- I changed it to HRP instead of Torso, this exists in all humanoids, Torso is R6 only. if range <= 80 then found = true if not active then active = true -- start chase end break -- breaks the for loop end end if not found then -- the entire for loop ran without finding anyone active = false -- stop chase end end