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

Does a CharacterAdded function run separately for each player?

Asked by 10 years ago

UPDATE: It works (LocalScript) now, which funnily. you can find in jav's answer, however, after the first time I change my class to an accepted Class (i.e. zombie, nighthunter, etc.), it displays a GUI, which respawns when you click a button. When I click it, it puts the GUI back. I've tried to work around this with the 'x' variable, but it still doesn't seem to work.

Hello. I've been writing a script, where, if a character respawns as certain 'classes' (a StringValue), the script will parent a Gui to them when they respawn.

I've shuffled between using Server-side scripts and LocalScripts, but LocalScripts don't seem to do it, correct me if I'm wrong; and the Server-side scripts is what I had a question about.

Does a CharacterAdded function run separately for each player? Meaning if I declared a variable 'x' (somewhat of a debounce variable), would that 'x' be tagged to that one player respawning, or would the 'x' be the same across all respawns?

Here's the code:

game.Players.PlayerAdded:connect(function(Player)
    x = "Stop"
    Player.CharacterAdded:connect(function(Character)
        wait()

        if x == "Go" then



                if Player.Class.Value == "Zombie" or Player.Class.Value == "Nighthunter" or Player.Class.Value == "Ogre" or Player.Class.Value == "Thing" or Player.Class.Value == "Imp" or Player.Class.Value == "Golem" or Player.Class.Value == "Savage" then
                    wait()
                    _G.StopPlr(Player)
                    if Player.PlayerGui:FindFirstChild("MonsterSelect") == nil then
                        local ms = game.ServerStorage.MonsterSelect:clone()
                        ms.Parent = Player.PlayerGui
                        x = "Stop"
                        else print("Defaulted to Zombie Class for ".. Player.Name)
                    end
                    else print("ERRRRRR")
                end

            --controls choose monster GUI
            else x = "Go"
        end

    end)
end)

Notice how I continually reference 'x' in the script? If someone respawns and x is 'Go', and then another person respawns, will that same x change, or will it be an entirely different x altogether? Sorry if that's a bit too confusing.

Thanks for reading; all replies are appreciated!

1 answer

Log in to vote
0
Answered by
jav2612 180
10 years ago

A LocalScript would probably be the best way to do this. Put the script in the player and do something like:


Player = game.Players.LocalPlayer x = "Stop" Player.CharacterAdded:connect(function(Character) wait() if x == "Go" then if Player.Class.Value == "Zombie" or Player.Class.Value == "Nighthunter" or Player.Class.Value == "Ogre" or Player.Class.Value == "Thing" or Player.Class.Value == "Imp" or Player.Class.Value == "Golem" or Player.Class.Value == "Savage" then wait() _G.StopPlr(Player) if Player.PlayerGui:FindFirstChild("MonsterSelect") == nil then local ms = game.ServerStorage.MonsterSelect:clone() ms.Parent = Player.PlayerGui x = "Stop" else print("Defaulted to Zombie Class for ".. Player.Name) end else print("ERRRRRR") end --controls choose monster GUI else x = "Go" end end)

That will make it much simpler instead of trying to keep track of variables for every single player. This way the script is focused only on the LocalPlayer.

0
I've tried that exact same thing, actually. It didn't do anything, and as of yet, I haven't found a noticeable issue. Tortelloni 315 — 10y
0
If there's no errors that probably means that the problem is elsewhere. I'm guessing it has something to do with your class system or whatever that is. You can try putting more prints in the script to see what is happening. jav2612 180 — 10y
0
I was able to get it to work, but with a drawback. Read the edit in my question. Tortelloni 315 — 10y
0
I just don't have enough information of all that is going on to be able to pinpoint the problem. jav2612 180 — 10y
Ad

Answer this question