Stopping Lag in game

I am a Roblox programmer and I have a slightly popular game ‘Bomb Mania’ As it increases in popularity the more and more I get complaints about the lag in game, it is getting to the point where the player count decreases significantly just due to lag, can someone help?

5 Likes

We do not have a context to work with to provide an advice, though, you can try a few stuff, doing stuff localy, not spamming while loops and other stuff

6 Likes

How would you like me to provide the context?

5 Likes

Like how do your codes look like are they piles of loops, are they like server sided for stuff like tweenservice, cframe and stuff (makes the game lags a lot watch videos of suphi kaner to understand more)

4 Likes

Try creating the bomb(s) or effects on the client instead of the server.

3 Likes

To help, this is my main code:

local players
local bomber = nil
local playereffect
local maps = game.ReplicatedStorage.Maps:GetChildren()
local votingSystem = game.Workspace.Voting
local firstPlace = nil
local secondPlace = nil
local thirdPlace = nil
local times = 0

local choices = votingSystem:GetChildren()

local function PickRandomMap()

	local randomNumber = math.random(1, #maps)

	randomMap = maps[randomNumber]

	return randomMap
end

while wait() do

	game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("Waiting For 3 Players...")

	workspace.InGame.Value = true

	repeat wait()
		players = game.Teams.In:GetPlayers()
	until #players >= 3

	if workspace:FindFirstChild("Crossroads") then
		workspace.Crossroads:Destroy()
	end

	if workspace:FindFirstChild("Rocket Arena") then
		workspace["Rocket Arena"]:Destroy()
	end

	if workspace:FindFirstChild("Techno Pad") then
		workspace["Techno Pad"]:Destroy()
	end

	if workspace:FindFirstChild("The Carnival") then
		workspace["The Carnival"]:Destroy()
	end

	if workspace:FindFirstChild("The Prison") then
		workspace["The Prison"]:Destroy()
	end
	
	if workspace:FindFirstChild("The Forest") then
		workspace["The Forest"]:Destroy()
	end
	
	if workspace:FindFirstChild("The Mansion") then
		workspace["The Mansion"]:Destroy()
	end

	game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("Starting Soon...")

	wait(30)

	game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("Vote For The Next Map!")

	workspace.VotingTime.Value = true

	for i, choice in pairs(choices) do

		if i == 1 then
			local name = choice.Sign.Paper.SurfaceGui.TextLabel
			local picture = choice.Sign.Paper.SurfaceGui.ImageLabel

			map1 = PickRandomMap()

			name.Text = randomMap.Name
			picture.Image = "rbxassetid://" .. randomMap.Image.Value
			randomMap.CanBeVoted.Value = true		

		elseif i == 2 then
			local name = choice.Sign.Paper.SurfaceGui.TextLabel
			local picture = choice.Sign.Paper.SurfaceGui.ImageLabel

			repeat wait()
				map2 = PickRandomMap()
			until map2 ~= map1

			name.Text = randomMap.Name
			picture.Image = "rbxassetid://" .. randomMap.Image.Value
			randomMap.CanBeVoted.Value = true	

		elseif i == 3 then
			local name = choice.Sign.Paper.SurfaceGui.TextLabel
			local picture = choice.Sign.Paper.SurfaceGui.ImageLabel

			repeat wait()
				map3 = PickRandomMap()
			until map3 ~= map2 and map3 ~= map1

			name.Text = randomMap.Name
			picture.Image = "rbxassetid://" .. randomMap.Image.Value
			randomMap.CanBeVoted.Value = true	
		end	
	end	

	wait(30)

	local Choice1Votes = votingSystem["Choice 1"].VotesAmount.Value
	local Choice2Votes = votingSystem["Choice 2"].VotesAmount.Value
	local Choice3Votes = votingSystem["Choice 3"].VotesAmount.Value

	if Choice1Votes >= Choice2Votes and Choice1Votes >= Choice3Votes then

		Map = game.ReplicatedStorage.Maps:FindFirstChild(votingSystem["Choice 1"].Sign.Paper.SurfaceGui.TextLabel.Text):Clone()
		
		if votingSystem["Choice 1"].Sign.Paper.SurfaceGui.TextLabel.Text == "The Carnival" then
			workspace.BGMusic.SoundId = "rbxassetid://1841682272"
		end
		
		if votingSystem["Choice 1"].Sign.Paper.SurfaceGui.TextLabel.Text == "Techno Pad" then
			workspace.BGMusic.SoundId = "rbxassetid://1842976958"
		end
		
		if votingSystem["Choice 1"].Sign.Paper.SurfaceGui.TextLabel.Text == "Rocket Arena" then
			workspace.BGMusic.SoundId = "rbxassetid://1847042482"
		end
		
		if votingSystem["Choice 1"].Sign.Paper.SurfaceGui.TextLabel.Text == "Crossroads" then
			workspace.BGMusic.SoundId = "rbxassetid://1845535642"
		end
		
		if votingSystem["Choice 1"].Sign.Paper.SurfaceGui.TextLabel.Text == "The Forest" then
			workspace.BGMusic.SoundId = "rbxassetid://1845205138"
		end
		
		if votingSystem["Choice 1"].Sign.Paper.SurfaceGui.TextLabel.Text == "The Mansion" then
			workspace.BGMusic.SoundId = "rbxassetid://1837768517"
		end

	elseif Choice2Votes >= Choice1Votes and Choice2Votes >= Choice3Votes then

		Map = game.ReplicatedStorage.Maps:FindFirstChild(votingSystem["Choice 2"].Sign.Paper.SurfaceGui.TextLabel.Text):Clone()
		
		if votingSystem["Choice 2"].Sign.Paper.SurfaceGui.TextLabel.Text == "The Carnival" then
			workspace.BGMusic.SoundId = "rbxassetid://1841682272"
		end
		
		if votingSystem["Choice 2"].Sign.Paper.SurfaceGui.TextLabel.Text == "Techno Pad" then
			workspace.BGMusic.SoundId = "rbxassetid://1842976958"
		end
		
		if votingSystem["Choice 2"].Sign.Paper.SurfaceGui.TextLabel.Text == "Rocket Arena" then
			workspace.BGMusic.SoundId = "rbxassetid://1847042482"
		end
		
		if votingSystem["Choice 2"].Sign.Paper.SurfaceGui.TextLabel.Text == "Crossroads" then
			workspace.BGMusic.SoundId = "rbxassetid://1845535642"
		end
		
		if votingSystem["Choice 2"].Sign.Paper.SurfaceGui.TextLabel.Text == "The Forest" then
			workspace.BGMusic.SoundId = "rbxassetid://1845205138"
		end
		
		if votingSystem["Choice 2"].Sign.Paper.SurfaceGui.TextLabel.Text == "The Mansion" then
			workspace.BGMusic.SoundId = "rbxassetid://1837768517"
		end

	else

		Map = game.ReplicatedStorage.Maps:FindFirstChild(votingSystem["Choice 3"].Sign.Paper.SurfaceGui.TextLabel.Text):Clone()
		
		if votingSystem["Choice 3"].Sign.Paper.SurfaceGui.TextLabel.Text == "The Carnival" then
			workspace.BGMusic.SoundId = "rbxassetid://1841682272"
		end
		
		if votingSystem["Choice 3"].Sign.Paper.SurfaceGui.TextLabel.Text == "Techno Pad" then
			workspace.BGMusic.SoundId = "rbxassetid://1842976958"
		end
		
		if votingSystem["Choice 3"].Sign.Paper.SurfaceGui.TextLabel.Text == "Rocket Arena" then
			workspace.BGMusic.SoundId = "rbxassetid://1847042482"
		end
		
		if votingSystem["Choice 3"].Sign.Paper.SurfaceGui.TextLabel.Text == "Crossroads" then
			workspace.BGMusic.SoundId = "rbxassetid://1845535642"
		end
		
		if votingSystem["Choice 3"].Sign.Paper.SurfaceGui.TextLabel.Text == "The Forest" then
			workspace.BGMusic.SoundId = "rbxassetid://1845205138"
		end
		
		if votingSystem["Choice 3"].Sign.Paper.SurfaceGui.TextLabel.Text == "The Mansion" then
			workspace.BGMusic.SoundId = "rbxassetid://1837768517"
		end

	end
	

	workspace.VotingTime.Value = false

	game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("Map Chosen - "..Map.Name)

	workspace.InGame.Value = false

	Map.Parent = workspace

	workspace.Voting["Choice 1"].button.Color = Color3.fromRGB(30, 111, 165)
	workspace.Voting["Choice 2"].button.Color = Color3.fromRGB(30, 111, 165)
	workspace.Voting["Choice 3"].button.Color = Color3.fromRGB(30, 111, 165)

	workspace.Voting["Choice 1"].VotesAmount.Value = 0
	workspace.Voting["Choice 1"].Sign.Paper.SurfaceGui.TextLabel.Text = "None"
	workspace.Voting["Choice 1"].Sign.Paper.SurfaceGui.ImageLabel.Image = ""
	workspace.Voting["Choice 1"].Votes.BillboardGui.TextLabel.Text = "0 Votes"
	workspace.Voting["Choice 2"].VotesAmount.Value = 0
	workspace.Voting["Choice 2"].Sign.Paper.SurfaceGui.TextLabel.Text = "None"
	workspace.Voting["Choice 2"].Sign.Paper.SurfaceGui.ImageLabel.Image = ""
	workspace.Voting["Choice 2"].Votes.BillboardGui.TextLabel.Text = "0 Votes"
	workspace.Voting["Choice 3"].VotesAmount.Value = 0
	workspace.Voting["Choice 3"].Sign.Paper.SurfaceGui.TextLabel.Text = "None"
	workspace.Voting["Choice 3"].Sign.Paper.SurfaceGui.ImageLabel.Image = ""
	workspace.Voting["Choice 3"].Votes.BillboardGui.TextLabel.Text = "0 Votes"
	game.ReplicatedStorage.RemoteEvents.ResetVotes:FireAllClients()
	
	if Map.Name == "The Forest" then
		game.ReplicatedStorage.RemoteEvents.CutsceneEvent:FireAllClients("The Forest")
		workspace.BGMusic.TimePosition = 0
		workspace.BGMusic.SoundId = "rbxassetid://1845538750"
		
		wait(7)

		workspace.BGMusic.SoundId = "rbxassetid://1845205138"
	end
	
	if Map.Name == "Rocket Arena" then
		game.ReplicatedStorage.RemoteEvents.CutsceneEvent:FireAllClients("Rocket Arena")
		workspace.BGMusic.TimePosition = 0
		workspace.BGMusic.SoundId = "rbxassetid://1845092181"

		wait(5)

		workspace.BGMusic.SoundId = "rbxassetid://1847042482"
	end
	
	if Map.Name == "Crossroads" then
		game.ReplicatedStorage.RemoteEvents.CutsceneEvent:FireAllClients("Crossroads")
		workspace.BGMusic.TimePosition = 0
		workspace.BGMusic.SoundId = "rbxassetid://1838782206"

		wait(10)

		workspace.BGMusic.SoundId = "rbxassetid://1845535642"
	end
	
	if Map.Name == "Techno Pad" then
		game.ReplicatedStorage.RemoteEvents.CutsceneEvent:FireAllClients("Techno Pad")
		workspace.BGMusic.TimePosition = 0
		workspace.BGMusic.SoundId = "rbxassetid://9043681154"

		wait(10)

		workspace.BGMusic.SoundId = "rbxassetid://1842976958"
	end
	
	if Map.Name == "The Carnival" then
		game.ReplicatedStorage.RemoteEvents.CutsceneEvent:FireAllClients("The Carnival")
		workspace.BGMusic.TimePosition = 0
		workspace.BGMusic.SoundId = "rbxassetid://1845069952"

		wait(10)

		workspace.BGMusic.SoundId = "rbxassetid://1841682272"
	end
	
	if Map.Name == "The Mansion" then
		game.ReplicatedStorage.RemoteEvents.CutsceneEvent:FireAllClients("The Mansion")
		workspace.BGMusic.TimePosition = 0
		workspace.BGMusic.SoundId = "rbxassetid://1838727070"

		wait(7)

		workspace.BGMusic.SoundId = "rbxassetid://1837768517"
	end
	
	workspace.BGMusic.TimePosition = 0

	for num, player in pairs(game.Teams.In:GetPlayers()) do
		local char = player.Character
		local val = math.random(1,18)

		game.ReplicatedStorage.RemoteEvents.CamType:FireClient(player, "Custom")

		game.ReplicatedStorage.RemoteEvents.StartGame:FireClient(player)

		for i, v in pairs(Map.Spawns:GetChildren()) do
			if i == val then
				char.Humanoid.Sit = false
				char.HumanoidRootPart.CFrame = v.CFrame
				char.Humanoid.WalkSpeed = 30
			end
		end
	end

	game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("Choosing Gamemode...")

	wait(4)

	if workspace.Modes:FindFirstChild("RapidFire") then
		local val = workspace.Modes:FindFirstChild("RapidFire")

		game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("Rapid-Fire Mode Has Been Activated By "..val.Value)

		workspace.BGMusic.PlaybackSpeed = 1.5

		val:Destroy()

		wait(4)

		game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("Gamemode Chosen - Bomb Tag")

		wait(3) 

		while wait() do

			if #game.Teams.In:GetPlayers() == 0 then
				game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("The Remaining Player Has Left...")

				wait(3)
				break
			end

			local value = math.random(1, #game.Teams.In:GetPlayers())

			if #game.Teams.In:GetPlayers() == 1 then
				for i, v in pairs(game.Teams.In:GetPlayers()) do
					firstPlace = v

					if thirdPlace ~= nil then
						local description = game:GetService("Players"):GetHumanoidDescriptionFromUserId(thirdPlace.UserId)
						if description then
							workspace["Third Place"].Humanoid:ApplyDescriptionReset(description)
							local nametag = game.ReplicatedStorage.HeadTag:Clone()
							nametag.Parent = workspace["Third Place"].Head
							nametag.User.Text = thirdPlace.Name
							nametag.Rank.Text = thirdPlace.leaderstats.Wins.Value.." Wins"
						end
					end
					if secondPlace ~= nil then
						local description = game:GetService("Players"):GetHumanoidDescriptionFromUserId(secondPlace.UserId)
						if description then
							workspace["Second Place"].Humanoid:ApplyDescriptionReset(description)
							local nametag = game.ReplicatedStorage.HeadTag:Clone()
							nametag.Parent = workspace["Second Place"].Head
							nametag.User.Text = secondPlace.Name
							nametag.Rank.Text = secondPlace.leaderstats.Wins.Value.." Wins"
						end
					end
					if firstPlace ~= nil then
						local description = game:GetService("Players"):GetHumanoidDescriptionFromUserId(firstPlace.UserId)
						if description then
							workspace["First Place"].Humanoid:ApplyDescriptionReset(description)
							local nametag = game.ReplicatedStorage.HeadTag:Clone()
							nametag.Parent = workspace["First Place"].Head
							nametag.User.Text = firstPlace.Name
							nametag.Rank.Text = firstPlace.leaderstats.Wins.Value.." Wins"
						end
					end

					game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(v.Name.." Won The Game!")
					workspace.BGMusic.SoundId = "rbxassetid://1844442520"
					workspace.BGMusic.TimePosition = 0
					game.ReplicatedStorage.RemoteEvents.CutsceneEvent:FireAllClients("Winner")
					workspace.BGMusic.PlaybackSpeed = 1
					v.Gold.Value = v.Gold.Value + 100
					if times == 3 then
						times = 0
					else
						times = times + 1
					end
					v.leaderstats.Wins.Value = v.leaderstats.Wins.Value + 1
					wait(8)
					workspace.BGMusic.SoundId = "rbxassetid://1835322551"
					v.Team = game.Teams.Out
					v.Character.Humanoid.Health = 0

					wait(2)

					for i, v in pairs(game.Players:GetPlayers()) do
						v.Team = game.Teams.In
					end

					firstPlace = nil
					secondPlace = nil
					thirdPlace = nil

				end
				break
			end

			for i, v in pairs(game.Teams.In:GetPlayers()) do

				local char = v.Character

				if value == i then

					if v.Team ~= game.Teams.In then
						return
					end

					bomber = v.Character
					playereffect = v
					v.Character.Humanoid.WalkSpeed = 50

					game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(bomber.Name.." Has The Bomb!")

					for i, v in pairs(playereffect.Inventory:GetChildren()) do
						if v:GetAttribute("Equipped")  then
							if v.Value == "Skin" then
								bomb = game.ReplicatedStorage.Skins:FindFirstChild(v.Name):Clone()
								bomb.Parent = workspace
							end
						end
					end

					--bomb.Position = v.Character.UpperTorso.Position + Vector3.new(0, -7, 0)

					local weld = Instance.new("Weld")
					weld.Name = "BombWeld"
					weld.Part0 = bomber.UpperTorso

					weld.Part1 = bomb
					weld.C0 = CFrame.new(0,0.3,-2.7)
					weld.C1 *= CFrame.Angles(math.pi/2,0,0)
					--weld.C0 = CFrame.new(v.Character.UpperTorso.Position, bomb.Position)

					weld.Parent = bomb


					local animation = Instance.new("Animation")  -- Create Animation object
					animation.AnimationId = "rbxassetid://13245017538"  -- Set the animation ID
					local animationTrack = bomber.Humanoid:LoadAnimation(animation)  -- Load the animation
					animationTrack:Play()  -- Play the animation

					bomb.Touched:Connect(function(hit)
						if hit.Parent:FindFirstChild("Humanoid") then
							if hit.Parent ~= bomber then
								local player = game.Players:GetPlayerFromCharacter(hit.Parent)
								if player then
									if player.Team == game.Teams.In then
										animationTrack:Stop()
										animation:Destroy()

										bomber.Humanoid.WalkSpeed = 30

										bomber = hit.Parent

										bomber.Humanoid.WalkSpeed = 50

										weld.Part0 = bomber.UpperTorso


										animation = Instance.new("Animation")  -- Create Animation object
										animation.AnimationId = "rbxassetid://13245017538"  -- Set the animation ID
										animationTrack = bomber.Humanoid:LoadAnimation(animation)  -- Load the animation
										animationTrack:Play()

										game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(bomber.Name.." Has The Bomb!")
										wait(0.5)
									end
								end
							end
						end
					end)

					local val = 0.7
					local val2 = math.random(-1.3,-0.9)

					while wait(val) do
						if bomb:FindFirstChild("Sound") then
							bomb:WaitForChild("Sound"):Play()
						end
						if bomb:FindFirstChild("PointLight") then
							bomb:WaitForChild("PointLight").Enabled = true
						end
						if bomb:FindFirstChild("Highlight") then
							bomb:WaitForChild("Highlight").Enabled = true
						end
						wait(val)
						if not bomber then
							return
						end
						val = val - 0.05
						if bomb:FindFirstChild("PointLight") then
							bomb:WaitForChild("PointLight").Enabled = false
						end
						if bomb:FindFirstChild("Highlight") then
							bomb:WaitForChild("Highlight").Enabled = false
						end
						if val < val2 then
							for i, v in pairs(playereffect.Inventory:GetChildren()) do
								if v:GetAttribute("Equipped")  then
									if v.Value == "Explosion" then
										effect = bomb:FindFirstChild(v.Name)
										if effect == nil then
											for i, v in pairs(playereffect.Inventory:GetChildren()) do
												if v.Value == "Explosion" then
													if v.Name == "Default" then
														effect = v
														for i, v in pairs(playereffect.Inventory:GetChildren()) do
															if v.Value == "Explosion" then
																if v.Name ~= "Default" then
																	v:SetAttribute("Equipped", false)
																end
															end
														end
													end
												end
											end
										end
									end
								end
							end

							if effect then
								for i, v in pairs(effect:GetChildren()) do
									if v:IsA("ParticleEmitter") then
										v:Emit(v:GetAttribute("EmitCount"))
									end
								end

								local sound = workspace:FindFirstChild(effect.Name)
								if sound:IsA("Sound") then
									sound:Play()
								end
							end


							if bomber then
								bomber.Humanoid:TakeDamage(bomber.Humanoid.Health)
							end

							if bomb then
								bomb.Transparency = 1

								for i, v in pairs(game.Teams.In:GetPlayers()) do
									game.ReplicatedStorage.RemoteEvents.ExplosionRE:FireClient(v, (bomb.Position - v.Character.HumanoidRootPart.Position).Magnitude)
								end
								wait(3)
								bomb:Destroy()
							end

							game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("The Bomb Has Exploded!")

							local playerbomber = game.Players:GetPlayerFromCharacter(bomber)
							if playerbomber then
								if #game.Teams.In:GetPlayers() == 2 then
									playerbomber.Gold.Value = playerbomber.Gold.Value + 25
									thirdPlace = playerbomber
								end
								if #game.Teams.In:GetPlayers() == 1 then
									playerbomber.Gold.Value = playerbomber.Gold.Value + 50
									secondPlace = playerbomber
								end
								if playerbomber then
									playerbomber.Team = game.Teams.Out
								end
							end

							wait(1)
							
							if #game.Teams.In:GetPlayers() > 1 then
								game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(#game.Teams.In:GetPlayers().." Players Remain")
							end

							break
						end

					end

					wait(4)
				end
			end
		end
		--[[
	elseif workspace.Modes:FindFirstChild("Boost") then
		local val = workspace.Modes:FindFirstChild("Boost")
		game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(val.Value.. " Has Bought The Boost Mode!")

		wait(3)

		workspace.BGMusic.SoundId = "rbxassetid://9048375035"

		game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("Gamemode Chosen - Bomb Tag")

		wait(3) 

		while wait() do

			if #game.Teams.In:GetPlayers() == 0 then
				game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("The Remaining Player Has Left...")
				wait(3)
				break
			end

			local value = math.random(1, #game.Teams.In:GetPlayers())

			if #game.Teams.In:GetPlayers() == 1 then
				for i, v in pairs(game.Teams.In:GetPlayers()) do
					game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(v.Name.." Won The Game!")
					workspace.BGMusic.PlaybackSpeed = 1
					workspace.BGMusic.SoundId = "rbxassetid://9048376021"
					v.Gold.Value = v.Gold.Value + 100
					v.leaderstats.Wins.Value = v.leaderstats.Wins.Value + 1
					wait(4)
					v.Team = game.Teams.Out

					wait(2)

					for i, v in pairs(game.Teams.Out:GetPlayers()) do
						v.Team = game.Teams.In
					end

				end
				break
			end

			for i, v in pairs(game.Teams.In:GetPlayers()) do

				local char = v.Character

				if value == i then

					if v.Team ~= game.Teams.In then
						return
					end

					bomber = v.Character
					if v.Character:FindFirstChild("SpeedBoost") then
						v.Character.Humanoid.WalkSpeed = 50
					else
						v.Character.Humanoid.WalkSpeed = 40
					end
					if v.Character:FindFirstChild("SlowBoost") then
						v.Character.Humanoid.WalkSpeed = 30
					else
						v.Character.Humanoid.WalkSpeed = 40
					end
				end

				game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(bomber.Name.." Has The Bomb!")

				local bomb = game.ReplicatedStorage.Bomb:Clone()
				local effects = game.ReplicatedStorage.Effects:Clone()
				effects.Weld.Part1 = bomb
				bomb.Parent = workspace
				effects.Parent = workspace
				--bomb.Position = v.Character.UpperTorso.Position + Vector3.new(0, -7, 0)

				local weld = Instance.new("Weld")
				weld.Name = "BombWeld"
				weld.Part0 = bomber.UpperTorso

				weld.Part1 = bomb
				weld.C0 = CFrame.new(0,0.3,-2.7)
				weld.C1 *= CFrame.Angles(math.pi/2,0,0)
				--weld.C0 = CFrame.new(v.Character.UpperTorso.Position, bomb.Position)

				weld.Parent = bomb


				local animation = Instance.new("Animation")  -- Create Animation object
				animation.AnimationId = "rbxassetid://13245017538"  -- Set the animation ID
				local animationTrack = bomber.Humanoid:LoadAnimation(animation)  -- Load the animation
				animationTrack:Play()  -- Play the animation

				bomb.Touched:Connect(function(hit)
					if hit.Parent:FindFirstChild("Humanoid") then
						if hit.Parent ~= bomber then
							local player = game.Players:GetPlayerFromCharacter(hit.Parent)
							if player.Team == game.Teams.In then
								animationTrack:Stop()
								animation:Destroy()
								if bomber:FindFirstChild("SpeedBoost") then
									bomber.Humanoid.WalkSpeed = 40
								else
									bomber.Humanoid.WalkSpeed = 30
								end
								if v.Character:FindFirstChild("SlowBoost") then
									v.Character.Humanoid.WalkSpeed = 20
								else
									v.Character.Humanoid.WalkSpeed = 30
								end


								bomber = hit.Parent

								if bomber:FindFirstChild("SpeedBoost") then
									bomber.Humanoid.WalkSpeed = 50
								else
									bomber.Humanoid.WalkSpeed = 40
								end
								if v.Character:FindFirstChild("SlowBoost") then
									v.Character.Humanoid.WalkSpeed = 30
								else
									v.Character.Humanoid.WalkSpeed = 40
								end


								weld.Part0 = bomber.UpperTorso


								local animation = Instance.new("Animation")  -- Create Animation object
								animation.AnimationId = "rbxassetid://13245017538"  -- Set the animation ID
								local animationTrack = bomber.Humanoid:LoadAnimation(animation)  -- Load the animation
								animationTrack:Play()

								game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(bomber.Name.." Has The Bomb!")
							end
						end
					end
				end)

				local val = 1
				local val2 = math.random(-1.3,-0.9)

				while wait(val) do
					bomb.Sound:Play()
					bomb.PointLight.Enabled = true
					bomb.Highlight.Enabled = true
					if bomber:FindFirstChild("RidBomb") then
						bomber:FindFirstChild("RidBomb"):Destroy()
						local val = math.random(1, #game.Teams.In:GetPlayers())

						for i, v in pairs(game.Teams.In:GetPlayers()) do
							if i == val then
								animationTrack:Stop()
								animation:Destroy()
								if bomber:FindFirstChild("SpeedBoost") then
									bomber.Humanoid.WalkSpeed = 40
								else
									bomber.Humanoid.WalkSpeed = 30
								end
								if v.Character:FindFirstChild("SlowBoost") then
									v.Character.Humanoid.WalkSpeed = 20
								else
									v.Character.Humanoid.WalkSpeed = 30
								end


								bomber = v.Character

								if bomber:FindFirstChild("SpeedBoost") then
									bomber.Humanoid.WalkSpeed = 50
								else
									bomber.Humanoid.WalkSpeed = 40
								end
								if v.Character:FindFirstChild("SlowBoost") then
									v.Character.Humanoid.WalkSpeed = 30
								else
									v.Character.Humanoid.WalkSpeed = 40
								end


								weld.Part0 = bomber.UpperTorso

								local animation = Instance.new("Animation")  -- Create Animation object
								animation.AnimationId = "rbxassetid://13245017538"  -- Set the animation ID
								local animationTrack = bomber.Humanoid:LoadAnimation(animation)  -- Load the animation
								animationTrack:Play()

								game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(bomber.Name.." Has The Bomb!")
							end
						end
					end
					for i, v in pairs(game.Teams.In:GetPlayers()) do
						if v.Character:FindFirstChild("CollectBomb") then
							v.Character.CollectBomb:Destroy()
							animationTrack:Stop()
							animation:Destroy()
							if bomber:FindFirstChild("SpeedBoost") then
								bomber.Humanoid.WalkSpeed = 40
							else
								bomber.Humanoid.WalkSpeed = 30
							end
							if v.Character:FindFirstChild("SlowBoost") then
								v.Character.Humanoid.WalkSpeed = 20
							else
								v.Character.Humanoid.WalkSpeed = 30
							end


							bomber = v.Character

							if bomber:FindFirstChild("SpeedBoost") then
								bomber.Humanoid.WalkSpeed = 50
							else
								bomber.Humanoid.WalkSpeed = 40
							end
							if v.Character:FindFirstChild("SlowBoost") then
								v.Character.Humanoid.WalkSpeed = 30
							else
								v.Character.Humanoid.WalkSpeed = 40
							end


							weld.Part0 = bomber.UpperTorso

							local animation = Instance.new("Animation")  -- Create Animation object
							animation.AnimationId = "rbxassetid://13245017538"  -- Set the animation ID
							local animationTrack = bomber.Humanoid:LoadAnimation(animation)  -- Load the animation
							animationTrack:Play()

							game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(bomber.Name.." Has The Bomb!")
						end
					end
					wait(val)
					if not bomber then
						return
					end
					val = val - 0.05
					bomb.PointLight.Enabled = false
					bomb.Highlight.Enabled = false
					if val < val2 then

						for i, v in pairs(effects:GetChildren()) do
							if v:IsA("ParticleEmitter") then
								v:Emit(v:GetAttribute("EmitCount"))
							end
						end
						workspace.Sound2:Play()
						bomber.Humanoid:TakeDamage(bomber.Humanoid.Health)
						bomb:Destroy()
						for i, v in pairs(game.Teams.In:GetPlayers()) do
							game.ReplicatedStorage.RemoteEvents.ExplosionRE:FireClient(v, (effects.Position - v.Character.HumanoidRootPart.Position).Magnitude)
						end
						wait(3)
						effects:Destroy() 

						game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("The Bomb Has Exploded!")

						local playerbomber = game.Players:GetPlayerFromCharacter(bomber)
						if playerbomber then
							if #game.Teams.In:GetPlayers() == 3 then
								playerbomber.Gold.Value = playerbomber.Gold.Value + 25
							end
							if #game.Teams.In:GetPlayers() == 2 then
								playerbomber.Gold.Value = playerbomber.Gold.Value + 50
							end
							if playerbomber then
								playerbomber.Team = game.Teams.Out
							end
						end

						wait(1)

						game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(#game.Teams.In:GetPlayers().." Players Remain")

						break
					end

				end

				wait(4)
			end
		end
--]]
	else
		game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("Gamemode Chosen - Bomb Tag")

		wait(3) 

		while wait() do

			if #game.Teams.In:GetPlayers() == 0 then
				game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("The Remaining Player Has Left...")

				wait(3)
				break
			end

			local value = math.random(1, #game.Teams.In:GetPlayers())

			if #game.Teams.In:GetPlayers() == 1 then
				for i, v in pairs(game.Teams.In:GetPlayers()) do
					firstPlace = v
					
					if thirdPlace ~= nil then
						local description = game:GetService("Players"):GetHumanoidDescriptionFromUserId(thirdPlace.UserId)
						if description then
							workspace["Third Place"].Humanoid:ApplyDescriptionReset(description)
							local nametag = game.ReplicatedStorage.HeadTag:Clone()
							nametag.Parent = workspace["Third Place"].Head
							nametag.User.Text = thirdPlace.Name
							nametag.Rank.Text = thirdPlace.leaderstats.Wins.Value.." Wins"
						end
					end
					if secondPlace ~= nil then
						local description = game:GetService("Players"):GetHumanoidDescriptionFromUserId(secondPlace.UserId)
						if description then
							workspace["Second Place"].Humanoid:ApplyDescriptionReset(description)
							local nametag = game.ReplicatedStorage.HeadTag:Clone()
							nametag.Parent = workspace["Second Place"].Head
							nametag.User.Text = secondPlace.Name
							nametag.Rank.Text = secondPlace.leaderstats.Wins.Value.." Wins"
						end
					end
					if firstPlace ~= nil then
						local description = game:GetService("Players"):GetHumanoidDescriptionFromUserId(firstPlace.UserId)
						if description then
							workspace["First Place"].Humanoid:ApplyDescriptionReset(description)
							local nametag = game.ReplicatedStorage.HeadTag:Clone()
							nametag.Parent = workspace["First Place"].Head
							nametag.User.Text = firstPlace.Name
							nametag.Rank.Text = firstPlace.leaderstats.Wins.Value.." Wins"
						end
					end
					
					game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(v.Name.." Won The Game!")
					workspace.BGMusic.SoundId = "rbxassetid://1844442520"
					workspace.BGMusic.TimePosition = 0
					game.ReplicatedStorage.RemoteEvents.CutsceneEvent:FireAllClients("Winner")
					workspace.BGMusic.PlaybackSpeed = 1
					v.Gold.Value = v.Gold.Value + 100
					if times == 3 then
						times = 0
					else
						times = times + 1
					end
					v.leaderstats.Wins.Value = v.leaderstats.Wins.Value + 1
					wait(8)
					workspace.BGMusic.SoundId = "rbxassetid://1835322551"
					v.Team = game.Teams.Out
					v.Character.Humanoid.Health = 0

					wait(2)

					for i, v in pairs(game.Players:GetPlayers()) do
						v.Team = game.Teams.In
					end
					
					firstPlace = nil
					secondPlace = nil
					thirdPlace = nil

				end
				break
			end

			for i, v in pairs(game.Teams.In:GetPlayers()) do

				local char = v.Character

				if value == i then

					if v.Team ~= game.Teams.In then
						return
					end

					bomber = v.Character
					playereffect = v
					v.Character.Humanoid.WalkSpeed = 40

					game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(bomber.Name.." Has The Bomb!")

					for i, v in pairs(playereffect.Inventory:GetChildren()) do
						if v:GetAttribute("Equipped")  then
							if v.Value == "Skin" then
								bomb = game.ReplicatedStorage.Skins:FindFirstChild(v.Name):Clone()
								bomb.Parent = workspace
							end
						end
					end

					--bomb.Position = v.Character.UpperTorso.Position + Vector3.new(0, -7, 0)

					local weld = Instance.new("Weld")
					weld.Name = "BombWeld"
					weld.Part0 = bomber.UpperTorso

					weld.Part1 = bomb
					weld.C0 = CFrame.new(0,0.3,-2.7)
					weld.C1 *= CFrame.Angles(math.pi/2,0,0)
					--weld.C0 = CFrame.new(v.Character.UpperTorso.Position, bomb.Position)

					weld.Parent = bomb


					local animation = Instance.new("Animation")  -- Create Animation object
					animation.AnimationId = "rbxassetid://13245017538"  -- Set the animation ID
					local animationTrack = bomber.Humanoid:LoadAnimation(animation)  -- Load the animation
					animationTrack:Play()  -- Play the animation

					bomb.Touched:Connect(function(hit)
						if hit.Parent:FindFirstChild("Humanoid") then
							if hit.Parent ~= bomber then
								local player = game.Players:GetPlayerFromCharacter(hit.Parent)
								if player then
									if player.Team == game.Teams.In then
										animationTrack:Stop()
										animation:Destroy()
										
										bomber.Humanoid.WalkSpeed = 30

										bomber = hit.Parent

										bomber.Humanoid.WalkSpeed = 40

										weld.Part0 = bomber.UpperTorso


										animation = Instance.new("Animation")  -- Create Animation object
										animation.AnimationId = "rbxassetid://13245017538"  -- Set the animation ID
										animationTrack = bomber.Humanoid:LoadAnimation(animation)  -- Load the animation
										animationTrack:Play()

										game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(bomber.Name.." Has The Bomb!")
										wait(0.5)
									end
								end
							end
						end
					end)

					local val = 1
					local val2 = math.random(-1.3,-0.9)

					while wait(val) do
						if bomb:FindFirstChild("Sound") then
							bomb:WaitForChild("Sound"):Play()
						end
						if bomb:FindFirstChild("PointLight") then
							bomb:WaitForChild("PointLight").Enabled = true
						end
						if bomb:FindFirstChild("Highlight") then
							bomb:WaitForChild("Highlight").Enabled = true
						end
						wait(val)
						if not bomber then
							return
						end
						val = val - 0.05
						if bomb:FindFirstChild("PointLight") then
							bomb:WaitForChild("PointLight").Enabled = false
						end
						if bomb:FindFirstChild("Highlight") then
							bomb:WaitForChild("Highlight").Enabled = false
						end
						if val < val2 then
							for i, v in pairs(playereffect.Inventory:GetChildren()) do
								if v:GetAttribute("Equipped")  then
									if v.Value == "Explosion" then
										effect = bomb:FindFirstChild(v.Name)
										if effect == nil then
											for i, v in pairs(playereffect.Inventory:GetChildren()) do
												if v.Value == "Explosion" then
													if v.Name == "Default" then
														effect = v
														for i, v in pairs(playereffect.Inventory:GetChildren()) do
															if v.Value == "Explosion" then
																if v.Name ~= "Default" then
																	v:SetAttribute("Equipped", false)
																end
															end
														end
													end
												end
											end
										end
									end
								end
							end
							
							if effect then
								for i, v in pairs(effect:GetChildren()) do
									if v:IsA("ParticleEmitter") then
										v:Emit(v:GetAttribute("EmitCount"))
									end
								end
								
								local sound = workspace:FindFirstChild(effect.Name)
								if sound:IsA("Sound") then
									sound:Play()
								end
							end

							
							if bomber then
								bomber.Humanoid:TakeDamage(bomber.Humanoid.Health)
							end
							
							if bomb then
								bomb.Transparency = 1

								for i, v in pairs(game.Teams.In:GetPlayers()) do
									game.ReplicatedStorage.RemoteEvents.ExplosionRE:FireClient(v, (bomb.Position - v.Character.HumanoidRootPart.Position).Magnitude)
								end
								wait(3)
								bomb:Destroy()
							end

							game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients("The Bomb Has Exploded!")

							local playerbomber = game.Players:GetPlayerFromCharacter(bomber)
							if playerbomber then
								if #game.Teams.In:GetPlayers() == 2 then
									playerbomber.Gold.Value = playerbomber.Gold.Value + 25
									thirdPlace = playerbomber
								end
								if #game.Teams.In:GetPlayers() == 1 then
									playerbomber.Gold.Value = playerbomber.Gold.Value + 50
									secondPlace = playerbomber
								end
								if playerbomber then
									playerbomber.Team = game.Teams.Out
								end
							end

							wait(1)

							game.ReplicatedStorage.RemoteEvents.TextLabel:FireAllClients(#game.Teams.In:GetPlayers().." Players Remain")

							break
						end

					end

					wait(4)
				end
			end
		end
	end
end

1 Like

You should probably make all the stuff involving the actual bomb being touched onto the client so it’s not as laggy. The animations being on the client will also help, and you should give time to load them before playing the animation.

The rest of the code, although messy, is not very bad performance wise.

1 Like

Thank you so much! One question, how would the bomb be touched to go on the client side? Would you want me to use a remote event when it explodes?

1 Like

Also, how would you want me to make it less messy?

1 Like

I haven’t really thought of anything but the explosion (or any particles) should be played on the client when the bomb explodes. The network owner of the bomb should also be set to the client. When the bomb is touched, it should explode on the client and send an event to the server to make it explode on everyone else’s client.

Also, the animations should be loaded on the client and played with a RemoteEvent. The animations should not be deleted.

That would be very hard to accomplish, but in short, just use tables and dictionaries for the maps and gamemode functions.

1 Like

1 More Question, there are issues where players would be touched from far away, and from the person with the bomb’s view, they were clearly touched will a touched event on the client fix that?

1 Like

From the player who dropped the bomb, it would look like it correctly hit the other player.

This particular issue should be fixed if you have it so if the player touches the bomb it explodes on their end.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.