Why isn't my code working but no error and it should be correct code?
I am making a piggy inspired game from scratch will the help of my friend, and he is the builder. I am the programmer of the game, and I am having some difficulties in my system that detects if the game is over or not. If the timer runs out it is over. If everyone dies or leaves the game, the round is over. If the slayer dies or leaves the game, the round is over. The timer part works completely fine, but the other 2 options work and don’t work. They work very well if you are doing 1 player, but not that good when multiple players get selected. I have gone over my code a bunch of times, and I even tried making it double-check, and still nothing. Voting works fine, but this glitch only happens when a player is selected and not a bot. Here is my round system code:
002 | for i, v in pairs (game.Players:GetChildren()) do |
003 | v.Character.Humanoid.Health = v.Character.Humanoid.MaxHealth |
006 | game.ReplicatedStorage.Status.Value = "<ContainerHorizontalAlignment=Center><AnimateStepFrequency=0>Intermission: <AnimateStyle=Fade><AnimateStyleTime=0.25><Color=Green>" .. i |
010 | local gameOver = false |
011 | local slayerLeft = false |
012 | local players = game.Players:GetChildren() |
013 | local chosenMode = "Player" |
014 | local numPlayers = #game.Players:GetChildren() |
015 | for i, v in pairs (players) do |
016 | if not v.Character then |
017 | numPlayers = numPlayers - 1 |
018 | table.remove(players, i) |
021 | if numPlayers = = 1 or #players = = 1 then |
024 | if #game.ReplicatedStorage.BotVote:GetChildren() > #game.ReplicatedStorage.PlayerVote:GetChildren() then |
027 | chosenMode = "Player" |
028 | numPlayers = numPlayers - 1 |
031 | if chosenMode = = "Player" then |
032 | slayer = game.Players:GetChildren() [ math.random( 1 , #game.Players:GetChildren()) ] |
034 | slayer = game.ServerStorage.Bots.Bot:Clone() |
036 | for i, v in pairs (players) do |
037 | if v.Name = = slayer.Name then |
038 | table.remove(players, i) |
040 | v.Character.Humanoid.WalkSpeed = 16 + v.ExtraSpeed.Value |
041 | v.Character.Humanoid.Died:Connect( function () |
042 | table.remove(players, i) |
043 | numPlayers = numPlayers - 1 |
045 | if v.Character:FindFirstChildOfClass( "Tool" ) then |
046 | game.ServerStorage.Tools:FindFirstChild(v.Character:FindFirstChildOfClass( "Tool" ).Name).Parent = game.Workspace.CurrentMap.Keys |
047 | v.Character:FindFirstChildOfClass( "Tool" ):Destroy() |
049 | if v.Backpack:FindFirstChildOfClass( "Tool" ) then |
050 | game.ServerStorage.Tools:FindFirstChild(v.Backpack:FindFirstChildOfClass( "Tool" ).Name).Parent = game.Workspace.CurrentMap.Keys |
051 | v.Backpack:FindFirstChildOfClass( "Tool" ):Destroy() |
055 | game.ReplicatedStorage.Escape.OnServerEvent:Connect( function (player) |
056 | print (player, "Escaped" ) |
057 | numPlayers = numPlayers - 1 |
058 | game.BadgeService:AwardBadge(player.UserId, 2124534311 ) |
059 | player.Points.Value = player.Points.Value + ( 35 * player.Multiplier.Value) |
060 | for i, v in pairs (players) do |
062 | table.remove(players, i) |
066 | game.Players.PlayerRemoving:Connect( function (player) |
067 | if player = = slayer then |
072 | for i, v in pairs (players) do |
074 | table.remove(players, i) |
079 | if chosenMode = = "Player" then |
080 | slayer.Character.Humanoid.Died:Connect( function () |
085 | game.ReplicatedStorage.Status.Value = "<ContainerHorizontalAlignment=Center><AnimateStepFrequency=0.05><AnimateStyle=Fade><AnimateStyleTime=0.25><Color=Red>" .. slayer.Name .. "<Color=/> has been selected as<Color=Red> Slayer!<Color=/>" |
087 | local map = game.ServerStorage.Maps.House:Clone() |
088 | map.Parent = game.Workspace |
089 | map.Name = "CurrentMap" |
090 | game.ReplicatedStorage.Status.Value = "<ContainerHorizontalAlignment=Center><AnimateStepFrequency=0.05><AnimateStyle=Fade><AnimateStyleTime=0.25>Loading Map..." |
092 | game.ReplicatedStorage.Fade:FireAllClients( true ) |
094 | game.ReplicatedStorage.Fade:FireAllClients( false ) |
095 | game.ReplicatedStorage.HideShop:FireAllClients( false ) |
097 | if chosenMode = = "Player" then |
098 | game.ReplicatedStorage.Backpack:FireClient(slayer, false ) |
100 | for i, v in pairs (players) do |
101 | game.ReplicatedStorage.Intro:FireClient(v) |
103 | game.ReplicatedStorage.Intro:FireClient(slayer) |
104 | for i, v in pairs (players) do |
107 | v.Character:SetPrimaryPartCFrame(CFrame.new( 266.253 , - 29.26 , - 93.261 ) * CFrame.Angles( 0 , math.rad( 90 ), 0 )) |
112 | game.Workspace.GameTimeMusic:Play() |
113 | if chosenMode = = "Player" then |
114 | skin = game.ServerStorage.Skins:FindFirstChild(slayer.Equip.Value):Clone() |
115 | skin.Name = slayer.Name |
116 | slayer.Character = skin |
117 | skin.Parent = game.Workspace |
118 | slayer.Character:SetPrimaryPartCFrame(CFrame.new( 339.263 , 4 , 205.29 )) |
119 | skin.Humanoid.WalkSpeed = 16 + slayer.ExtraSpeed.Value |
121 | for i, v in pairs (players) do |
122 | if v.Name ~ = slayer.Name then |
124 | v.Character:SetPrimaryPartCFrame(map.Spawn.CFrame) |
128 | game.ReplicatedStorage.CurrentSlayer.Value = slayer.Name |
129 | for i = 480 , 0 , - 1 do |
130 | local timer = math.floor(i / 60 ) .. ":" .. i % 60 |
132 | timer = math.floor(i / 60 ) .. ":0" .. i % 60 |
135 | if chosenMode = = "Player" then |
136 | slayer.Character:SetPrimaryPartCFrame(map.Spawn.CFrame) |
138 | slayer.Parent = game.Workspace |
139 | slayer:SetPrimaryPartCFrame(map.Spawn.CFrame) |
142 | game.ReplicatedStorage.Status.Value = "<ContainerHorizontalAlignment=Center><AnimateStepFrequency=0>Game Time: <AnimateStyle=Fade><AnimateStyleTime=0.25><Color=Green>" .. timer |
144 | if #players = = 0 then |
147 | if numPlayers = = 0 then |
154 | game.Workspace.GameTimeMusic:Stop() |
155 | game.ReplicatedStorage.Status.Value = "<ContainerHorizontalAlignment=Center><AnimateStepFrequency=0.05><AnimateStyle=Fade><AnimateStyleTime=0.25>Game Over!" |
157 | game.ReplicatedStorage.Backpack:FireAllClients( true ) |
158 | game.Workspace.TrapsPlacement:ClearAllChildren() |
160 | if not slayerLeft then |
161 | if chosenMode = = "Player" then |
162 | slayer:LoadCharacter() |
168 | game.ReplicatedStorage.CurrentSlayer.Value = "" |
169 | game.ReplicatedStorage.HideShop:FireAllClients( true ) |
170 | for i, v in pairs (players) do |
172 | v.Character:MoveTo(game.Workspace:FindFirstChild( "SpawnLocation" .. math.random( 1 , 3 )).Position) |
175 | game.ReplicatedStorage.BotVote:ClearAllChildren() |
176 | game.ReplicatedStorage.PlayerVote:ClearAllChildren() |
177 | game.ServerStorage.Tools:ClearAllChildren() |
I just cannot figure out why it doesn’t work. I sometimes found that if a player dies it sometimes doesn’t do anything. Is the script not efficient or is it an error? I can’t seem to know!