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

Variable stuck inside the function?

Asked by 8 years ago
Edited 8 years ago

Ok, i'm not the best scripter in the world and I basically taught myself through roblox wiki and such. In the game i'm currently scripting, I decided I wanted to have guis for different outcomes in the game. But being someone who has never scripted something like this before I had to kinda test all sorts of ways to get this to work. Finally, I discovered that if I used the function,

for _, player in pairs(game.Players:GetPlayers()) do
        if player and player.Character then
            CurrentPlayers = player
        end
CurrentPlayers.PlayerGui.GameGUIs.MorePlayers.Visible = false
end

That it actually showed up on the players screens! Of course I didn't make the entire thing by myself and snagged the

local PlayerFunction = function()
    for _, player in pairs(game.Players:GetPlayers()) do
        if player and player.Character then

from a tutorial I saw. I had to try and learn how it works but it is incredibly confusing for me so I just let it be. After I put the script above ^ in all the required places my game actually worked! But unfortunately the game had lots of lag. I tried getting rid of all those annoying bug notifications of scripts I borrowed but it didn't work. Then I decided if I called the function above instead of pasting it everywhere then that might help. So that is what i'm trying to do!

I'm not even sure if this will help with the lag bug at this point I have tried to hard to make it work that I want to see if I even can. Right now I have this at the beginning of my script that I call when needed.

local PlayerFunction = function()
    for _, player in pairs(game.Players:GetPlayers()) do
        if player and player.Character then
            CurrentPlayers = player
        end
    end
end

When I get to a point where I need to show guis for the players, I just do this.

PlayerFunction()
CurrentPlayers.PlayerGui.GameGUIs.MorePlayers.Visible = false
CurrentPlayers.PlayerGui.GameGUIs.NumberPlayers.Visible = false

In my head this worked perfectly. But as a matter of fact it did not do that and ever since I have been trying to figure out what was wrong. The CurrentPlayers variable I defined in the function is evidently not global for some reason and when I test the script it says that CurrentPlayers is not defined.

I have tried all sorts of way to make this work like putting the function in a separate script and calling it and even making the CurrentPlayers variable get pasted to a string value, then pulling that value back to call the guis. Of course, that didn't work and it was only registered as useless string, not the player values I needed in order to call their gui folders.

Please help me! Again i'm new at this so don't cringe if I did something terribly wrong.

0
I think that this http://wiki.roblox.com/index.php?title=Scope would help you fix your problem. I hope you understand that there's different levels of scopes // floors , and ... maybe that's the reason why CurrentPlayers is an unkown global! It's in a different scope ;D! iDarkGames 483 — 8y
0
Wow I didn't realize that. Thanks! FrogNinjaX 222 — 8y

Answer this question