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

Every 3 second random player gets random effect. Instead it happens once. How to fix?

Asked by
MAKKU 37
5 years ago
eventss = {"smoke","fire","sparkles"}



game.Players.PlayerAdded:connect(function(player)
while true do
for i,v in pairs (eventss) do
    player.CharacterAdded:wait()
local randomPlayer = game.Players:GetPlayers()
[math.random(1,#game.Players:GetPlayers())]
print(randomPlayer)
    print(i,v)
    if v == "smoke" then
        local a = Instance.new("Smoke",randomPlayer.Character.Head)
        wait(3)
        a:Destroy()
    elseif
        v == "sparkles" then
        local b = Instance.new("Sparkles",randomPlayer.Character.Head)
        wait(3)
        b:Destroy()
        elseif
        v == "fire" then
        local c = Instance.new("Fire",randomPlayer.Character.Head)
        wait(3)
        c:Destroy()
    end
    end
end
end)
0
pstt Async_io 908 — 5y
0
game:GetService("Debris"):AddItem() Async_io 908 — 5y
0
might wanna look it up ;) Async_io 908 — 5y

2 answers

Log in to vote
-1
Answered by 5 years ago

You're executing this for every player wich you shouldn't be doing. This script should work:

while wait(3) do --loops every 3 seconds
    pcall(function() -- acts as safe guard so if an error occors it wont break
        while wait() do -- repeats until it finds an alive character
            local randomnumber = math.random(1,#game:GetService'Players':GetChildren()) --selects random player
            local plr = game:GetService'Players':GetChildren()[randomnumber] -- converts number to player
            if plr.Character then
                if plr.Character.Head then
                    local randomnumber2 = math.random(1,3) -- generates a random particle
                    if randomnumber2 == 1 then
                        local c = Instance.new('Smoke', plr.Character.Head)
                        game:GetService'Debris':AddItem(c, 3) --request to remove after 3 seconds
                        elseif randomnumber2 == 2 then
                        local c = Instance.new('Fire', plr.Character.Head)
                        game:GetService'Debris':AddItem(c, 3) --request to remove after 3 seconds
                        elseif randomnumber2 == 3 then
                        local c = Instance.new('ParticleEmitter', plr.Character.Head)
                        game:GetService'Debris':AddItem(c, 3) --request to remove after 3 seconds
                    end
                    break--stops the while loop since it successfully added the particle
                end
            end
        end
    end)
end
0
Just why. User#5423 17 — 5y
0
What exactly did i do wrong? ThisIsAnAccount255 143 — 5y
0
There is no reason to use pcall in this code at all. User#5423 17 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

You are overcomplicating this all you need in the loop is to check if the Player has a character remove old effects if there is one then add a random one.

Your code is an infinate loop inside the player added event which is bad on its own. You also do not use the Player and instead randoly get a playe from the game to change the effect on. These are tow differnt things one if for the playre who joined and the other is getting all the players?

Just use a loop for all player in the game.

Example:-

local itms = {"Smoke","Fire","Sparkles"}
local function getEffect(head)

    -- remove all items in the list
    for i=1, #itms do
        local tmpItm = head:FindFirstChild(itms[i])
        if tmpItm  then
            tmpItm:Destroy()
        end
    end

    -- add random item
    Instance.new(itms[math.random(1, #itms)], head)
end

-- main  loop
while true do
    wait(3)

    -- check each player in the game
    for _, plr in pairs(game.Players:GetPlayers()) do
        if plr.Character and plr.Character.Head then
            getEffect(plr.Character.Head)
        end
    end
end

I hope this helps.

Answer this question