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

More efficient way to do this?

Asked by
wjs3456 90
10 years ago

I sort of lied, this is a two part question.

Info:I know this is in a global function, I call it in another script. Also the part with the Screen Gui is alright so that you shouldn't need to worry about.

_G["survivor"] = function()
    for i,v in pairs(game.Players:GetPlayers()) do
        v.PlayerGui.ScreenGui.survivor.Visible = true
        v.PlayerGui.ScreenGui.survivor.Visible = true
        v.PlayerGui.ScreenGui.survivor.Visible = true
        v.PlayerGui.ScreenGui.survivor.Visible = true
        v.PlayerGui.ScreenGui.survivor.Visible = true
        v.PlayerGui.ScreenGui.survivor.Visible = true
        v.PlayerGui.ScreenGui.survivor.Visible = true
        v.PlayerGui.ScreenGui.survivor.Visible = true
        v.PlayerGui.ScreenGui.survivor.Visible = true
        v.PlayerGui.ScreenGui.survivor.Visible = true
        v.PlayerGui.ScreenGui.survivor.Visible = true
        v.PlayerGui.ScreenGui.survivor.Visible = true
        wait(4)
        v.PlayerGui.ScreenGui.survivor.Visible = false
        v.PlayerGui.ScreenGui.survivor.Visible = false
        v.PlayerGui.ScreenGui.survivor.Visible = false
        v.PlayerGui.ScreenGui.survivor.Visible = false
        v.PlayerGui.ScreenGui.survivor.Visible = false
        v.PlayerGui.ScreenGui.survivor.Visible = false
        v.PlayerGui.ScreenGui.survivor.Visible = false
        v.PlayerGui.ScreenGui.survivor.Visible = false
        v.PlayerGui.ScreenGui.survivor.Visible = false
        v.PlayerGui.ScreenGui.survivor.Visible = false
        v.PlayerGui.ScreenGui.survivor.Visible = false
        v.PlayerGui.ScreenGui.survivor.Visible = false
        wait(1)
    end
end

1) How can I eliminate the excessive amounts of the same lines. I'm sure you can use a for loop but, I am not very good with them so if anyone can explain that would be great.

2)Whenever I test it with another player it will show up on the first person's screen, wait for it to finish, then goes to the next person, until it has done all the players. Is there a way to make it appear all at one time?

I really hope this makes some sense. Thanks :)

1
Why exactly are you using the same line over 10 times? bbissell 346 — 10y
0
Because for some reason I ran into a strange problem(never did solve) where only one person could see it so when I do it over and over it works. wjs3456 90 — 10y

2 answers

Log in to vote
1
Answered by
bbissell 346 Moderation Voter
10 years ago

If you have two seperate loops, It will make everyone's visible, Then wait 4 seconds and make it invisible.

_G["survivor"] = function()
    for i,v in pairs(game.Players:GetPlayers()) do
        v.PlayerGui.ScreenGui.survivor.Visible = true
    end
        wait(4)
    for i,v in pairs(game.Players:GetPlayers()) do
        v.PlayerGui.ScreenGui.survivor.Visible = false
    end
end

Im not sure why you had to repeatable spam those lines of code, This should work if there is only one frame.

0
Why do I have to do two for loops? wjs3456 90 — 10y
0
The First loop will make everyone's survivor frame visible all at once, The Second loop will make everyone's survivor frame invisible after 4 Seconds bbissell 346 — 10y
Ad
Log in to vote
-3
Answered by 10 years ago

Well if you want to do this for more than one player at a time you could use threads

Threads allow you to run multiple pieces of code at once

In ROBLOX there is a built in function called Spawn that allows you to create threads

Example:

In this first code I don't use threading so it prints every 2 seconds

for i = 1, 10 do
    print(i)
    wait(2)
    print(i)
end

In the second example I use threading so it prints all at once instead of waiting 2 seconds in between each one

for i = 1,10 do
    Spawn(function()
        print(i)
        wait(2)
        print(i)
    end)
end

So just apply threading to your code and it should work.

Also there is no reason you should need to set the same value 10 times it will stay the same you only need to set it once.

Answer this question