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

something other than while true do ?

Asked by 5 years ago
Edited 5 years ago

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

0
you can use repeat ... until false for infinite loops as well User#19524 175 — 5y

1 answer

Log in to vote
0
Answered by
gullet 471 Moderation Voter
5 years ago
Edited 5 years ago

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
0
If you want the sound played for one person you need to play it in a localscript, you can still have it triggered on the server if you use a remote to tell a localscript to play it locally. gullet 471 — 5y
0
thanks you for helping !!!! sheppard929 9 — 5y
0
I'm not sure if this is exactly what you wanted but perhaps it gives some insight nonetheless gullet 471 — 5y
0
for the sound, I'll handle it, but you fixed two problems for me. online, the script didn't work and the sound works! :D sheppard929 9 — 5y
Ad

Answer this question