For i,v in pairs loop breaking game

  1. I want to fix my script, so I can continue making my game.

  2. When I sell and there is a block assigned to another person, a script breaks. I don’t know where to locate it

  3. I have found the solution (break function breaks loops) but don’t know how and where to fix it. Please help!

Images and Code

[1.]
image

local Tool
local MaxBlocks,CurrentBlocks,Cash,CashMulti
local Short = require(game.ReplicatedStorage.Short)

local DB = false
local CashStart = false

local Data = game:GetService("DataStoreService"):GetDataStore("TheDS1Testing")
local Players = game:GetService("Players")
local Connections = {}

local function ExecuteData(Player)
	local Success, Stats, Error = pcall(function() return Data:GetAsync(tostring(Player.UserId)) end)
	if not Success and Error then
		warn(Error)
	else
		local StatFolder = Instance.new("Folder")
		StatFolder.Name = "Stats"
		local MaxBlocksReal = Instance.new("NumberValue")
		MaxBlocksReal.Name = "MaxBlocks"
		local CurrentBlocksReal = Instance.new("NumberValue")
		CurrentBlocksReal.Name = "CurrentBlocks"
		local CashReal = Instance.new("NumberValue")
		CashReal.Name = "Cash"
		local CashMultiReal = Instance.new("NumberValue")
		CashMultiReal.Name = "CashMulti"
		local Cooldown = Instance.new("NumberValue")
		Cooldown.Name = "Cooldown"

		MaxBlocksReal.Value = Stats and Stats[1] or 10
		CurrentBlocksReal.Value = Stats and Stats[2] or 0
		CashReal.Value = Stats and Stats[3] or 0
		CashMultiReal.Value = Stats and Stats[4] or 1
		Cooldown.Value = Stats and Stats[5] or 0.1
		
		MaxBlocksReal.Parent = StatFolder
		CurrentBlocksReal.Parent = StatFolder
		CashReal.Parent = StatFolder
		CashMultiReal.Parent = StatFolder
		Cooldown.Parent = StatFolder
		StatFolder.Parent = Player

		while wait(0.01) do 
			MaxBlocks = MaxBlocksReal
			CurrentBlocks = CurrentBlocksReal
			Cash = CashReal
			CashMulti = CashMultiReal
		end
	end		
end	
game.Players.PlayerAdded:Connect(ExecuteData)

Players.PlayerRemoving:Connect(function(Player)
	if Connections[Player] then Connections[Player]:Disconnect() end
	local Folder = Player:FindFirstChild("Stats")
	local Success, Error = pcall(function() 
		Data:SetAsync(tostring(Player.UserId),{
			Folder.MaxBlocks.Value,
			Folder.CurrentBlocks.Value,
			Folder.Cash.Value,
			Folder.CashMulti.Value,
			Folder.Cooldown.Value
		})
	end)
	if not Success then
		warn(Error)
	else
		Folder:Destroy()
	end
end)

game:BindToClose(function()
	wait(1)
end)

local function CashGuiUpdate(Player)
	while wait(0.1) do
		Player:WaitForChild("PlayerGui").trm.cash.Text = "Cash: $"..Short.en(Cash.Value)
	end	
end

game.ReplicatedStorage.ToolActivated.OnServerEvent:Connect(function(Player)
Tool = Player.Character:WaitForChild("Block")
Tool.Activated:Connect(function()
	local Character = Player.Character
		local trm = Player.PlayerGui.trm
	if CashStart == false then
		CashStart = true
		wait(0.1)
		CashGuiUpdate(Player)
	end

	
	if CurrentBlocks.Value < MaxBlocks.Value then
		if DB == false then
			DB = true	
			CurrentBlocks.Value += 1
			local Block = Tool.Handle:Clone()
			Block.Name = "BlockClone"

			Block.Anchored = true
			Block.Position = Vector3.new(Character.HumanoidRootPart.Position.X,0.75,Character.HumanoidRootPart.Position.Z)
			Block.Orientation = Vector3.new(0,0,0)
			Block.Parent = workspace
			Block:SetAttribute("Owner",Player.Name)
			Player:WaitForChild("PlayerGui").trm.TextLabel.Text = "Blocks: "..Short.en(CurrentBlocks.Value).."/"..Short.en(MaxBlocks.Value)
			wait(Player:WaitForChild("Stats").Cooldown.Value)
			DB = false
		   end	
	     end	
	 end)	
end)	

[2.]
image

script.Parent.Activated:Connect(function()
	game.ReplicatedStorage.ToolActivated:FireServer()
end)

[3.]
image

local CurrentBlocks
local MaxBlocks
local Cash
local CashMulti

local Short = require(game.ReplicatedStorage.Short)

local function CheckForDelete(Player)
	if CurrentBlocks.Value <= MaxBlocks.Value and CurrentBlocks.Value > 0 then
		for i,v in pairs(game.Workspace:GetChildren()) do
			if v.Name == "BlockClone" then
				if v:GetAttribute("Owner") == Player.Name then
					v:Destroy()
					CurrentBlocks.Value = 0
					Player:WaitForChild("PlayerGui").trm.TextLabel.Text = "Blocks: "..Short.en(CurrentBlocks.Value).."/"..Short.en(MaxBlocks.Value)
				else
					repeat wait()
						for i,v in pairs(game.Workspace:GetChildren()) do
							if v.Name == "BlockClone" then
								if v:GetAttribute("Owner") == Player.Name then
									v:Destroy()
									CurrentBlocks.Value = 0
									Player:WaitForChild("PlayerGui").trm.TextLabel.Text = "Blocks: "..Short.en(CurrentBlocks.Value).."/"..Short.en(MaxBlocks.Value)
								end
							end
						end	
					until
					v:GetAttribute("Owner") == Player.Name
				end	
			end		
		end	
	end
end	

local DB2 = false
script.Parent.Touched:Connect(function(hit)
	local player2 = game.Players:GetPlayerFromCharacter(hit.Parent)
	CurrentBlocks = player2:WaitForChild("Stats").CurrentBlocks
	MaxBlocks = player2:WaitForChild("Stats").MaxBlocks
	Cash = player2:WaitForChild("Stats").Cash
	CashMulti = player2:WaitForChild("Stats").CashMulti
		wait(0.1)
	if CurrentBlocks.Value <= MaxBlocks.Value and CurrentBlocks.Value > 0 then
		if DB2 == false then
			DB2 = true
			Cash.Value += CurrentBlocks.Value * CashMulti.Value
			CurrentBlocks.Value = 0
			CheckForDelete(player2)
			wait(0.2)
			DB2 = false
		end	
	end	
end)	

[4.]
image

local CurrentBlocks
local MaxBlocks
local Cash
local CashMulti

local Short = require(game.ReplicatedStorage.Short)

local function CheckForDelete(Player)
	if CurrentBlocks.Value <= MaxBlocks.Value then
		for i,v in pairs(game.Workspace:GetChildren()) do
			if v.Name == "BlockClone" then
				if v:GetAttribute("Owner") == Player.Name then
					v:Destroy()
					CurrentBlocks.Value = 0
					Player:WaitForChild("PlayerGui").trm.TextLabel.Text = "Blocks: "..Short.en(CurrentBlocks.Value).."/"..Short.en(MaxBlocks.Value)
				else
					repeat wait()
						for i,v in pairs(game.Workspace:GetChildren()) do
							if v.Name == "BlockClone" then
								if v:GetAttribute("Owner") == Player.Name then
									v:Destroy()
									CurrentBlocks.Value = 0
									Player:WaitForChild("PlayerGui").trm.TextLabel.Text = "Blocks: "..Short.en(CurrentBlocks.Value).."/"..Short.en(MaxBlocks.Value)
								end
							end
						end	
					until
					v:GetAttribute("Owner") == Player.Name
				end	
			end		
		end	
	end
end	

local DB2 = false
script.Parent.Touched:Connect(function(hit)
	local player2 = game.Players:GetPlayerFromCharacter(hit.Parent)
	CurrentBlocks = player2:WaitForChild("Stats").CurrentBlocks
	MaxBlocks = player2:WaitForChild("Stats").MaxBlocks
	Cash = player2:WaitForChild("Stats").Cash
	CashMulti = player2:WaitForChild("Stats").CashMulti
	if Cash.Value >= MaxBlocks.Value * 25 then
		
			if DB2 == false then
				DB2 = true
				wait(0.1)
				MaxBlocks.Value *= 2
				Cash.Value = 0
				CurrentBlocks.Value = 0
			player2:WaitForChild("PlayerGui").trm.TextLabel.Text = "Blocks: "..Short.en(CurrentBlocks.Value).."/"..Short.en(MaxBlocks.Value)
				CheckForDelete(player2)
				wait(0.2)
				DB2 = false
		end
	end
end)	

[5.]
image

local CurrentBlocks
local MaxBlocks
local Cash
local CashMulti

local Short = require(game.ReplicatedStorage.Short)

local function CheckForDelete(Player)
	if CurrentBlocks.Value <= MaxBlocks.Value then
		for i,v in pairs(game.Workspace:GetChildren()) do
			if v.Name == "BlockClone" then
				if v:GetAttribute("Owner") == Player.Name then
					v:Destroy()
					CurrentBlocks.Value = 0
					Player:WaitForChild("PlayerGui").trm.TextLabel.Text = "Blocks: "..Short.en(CurrentBlocks.Value).."/"..Short.en(MaxBlocks.Value)
				else
					repeat wait()
						for i,v in pairs(game.Workspace:GetChildren()) do
							if v.Name == "BlockClone" then
								if v:GetAttribute("Owner") == Player.Name then
									v:Destroy()
									CurrentBlocks.Value = 0
									Player:WaitForChild("PlayerGui").trm.TextLabel.Text = "Blocks: "..Short.en(CurrentBlocks.Value).."/"..Short.en(MaxBlocks.Value)
								end
							end
						end	
					until
					v:GetAttribute("Owner") == Player.Name
				end	
			end		
		end	
	end
end	

local DB2 = false
script.Parent.Touched:Connect(function(hit)
	local player2 = game.Players:GetPlayerFromCharacter(hit.Parent)
	CurrentBlocks = player2:WaitForChild("Stats").CurrentBlocks
	MaxBlocks = player2:WaitForChild("Stats").MaxBlocks
	Cash = player2:WaitForChild("Stats").Cash
	CashMulti = player2:WaitForChild("Stats").CashMulti
	if Cash.Value >= MaxBlocks.Value * 75 then
		
			if DB2 == false then
				DB2 = true
				wait(0.1)
				CashMulti.Value += 1
				Cash.Value = 0
				CurrentBlocks.Value = 0
			player2:WaitForChild("PlayerGui").trm.TextLabel.Text = "Blocks: "..Short.en(CurrentBlocks.Value).."/"..Short.en(MaxBlocks.Value)
				CheckForDelete(player2)
				wait(0.2)
				DB2 = false
		end
	end
end)	

Thank you for putting the time into reading this!!

1 Like

just do it for every server debounce