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!
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.