function onPlayerEntered(newPlayer) local stat = Instance.new("BoolValue",newPlayer) stat.Name = "Playing" stat.Value = false end game.Players.ChildAdded:connect(onPlayerEntered) while true do for _,v in pairs (game.Players:GetPlayers()) do for _,v in pairs (v:GetChildren()) do if v.Name == "Playing" then if v.Value == true then script.Parent.Value = script.Parent.Value + 1 end end end end wait() end
How do I make it so it doesn't keep adding onto the numbervalue? I know this has to do with the while true do, but is there a certain loop that i can use that doesn't repeat previous ones?
I see by your coding style, you are setting up your game to fail in the long run. As an example, I am going to provide you some code which: 1) Could improve your coding style. 2) Create a more secure game.
local PlayerManager={} do PlayerManager.RegisteredPlayers={}; local Methods={}; local Metatable={__index=Methods}; function Methods:Destroy() PlayerManager.RegisteredPlayers[self.UserId]=nil; end; function Methods:IsPlaying() return self.Data.IsPlaying; end; function Methods:ToggleIsPlaying() self.Data.IsPlaying=not self.Data.IsPlaying; return self:IsPlaying(); end; local function RegisterNewPlayer(Player) local Data={}; Data.IsPlaying=false; Data.Score=0; Data.UserId=Player.userId; return setmetatable({Data=Data},Metatable); end; function PlayerManager:Get(Player) local RegisteredPlayer=self.RegisteredPlayers[Player.userId]; if (not RegisteredPlayer) then RegisteredPlayer=RegisterNewPlayer(Player); self.RegisteredPlayers[Player.userId]=RegisteredPlayer; end; return RegisteredPlayer; end; end; local NewPlayer; game:GetService("Players").PlayerAdded:connect(function(Player) NewPlayer=PlayerManager:Get(Player); print("New player ".. Player.Name .." has joined the game!"); if (GameEventIsOccuring) then -- example NewPlayer:ToggleIsPlaying(); end; end); game:GetService("Players").PlayerRemoving:connect(function(Player) PlayerManager:Get(Player):Destroy(); end);
If you have any questions or concerns on what my code does, feel free to