Issues With Property Change, Debounce Not Working

I am attempting to make a Tycoon based off a tutorial while also making my own changes for my preferences and this script is for handling the tycoon. The present section that is currently seperated by the “—” is the area in which it occurs. When called on, it is to make the players “Income” property add up for the money loop script but instead, it does it (X) Number of times due to delay, and debounce is not working for this as it still happens.


local TemplatePlot = game.Workspace.TemplatePlot

game.Players.PlayerAdded:Connect(function(player)
	
	for _, plot in Plots:GetChildren() do
		if plot:GetAttribute("Taken") then continue end
		plot:SetAttribute("Taken", true) 
		plot:SetAttribute("Owner", player.UserId)
		
		print("Plot has been given to"..player.Name)
		
		local ItemsFolder = Instance.new("Folder")
		ItemsFolder.Name = "Items"
		ItemsFolder.Parent = plot
		
		
		local TemplateButtons = TemplatePlot.Buttons:Clone()
		local TemplateItems = TemplatePlot.Items
		
		for _, Button in TemplateButtons:GetChildren() do
			local RelativeCFrame = TemplatePlot.CFrame:ToObjectSpace(Button.CFrame)
			Button.CFrame = plot.CFrame:ToWorldSpace(RelativeCFrame)
			
			Button.Touched:Connect(function(hit)
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if not player then return end
				if plot:GetAttribute("Owner") ~= player.UserId  then return end
				
				local Price = Button:GetAttribute("Price")
				
				if Price then
					if player.leaderstats.Cash.Value < Price then
						warn("You are a broke person")
						return
					end
					player.leaderstats.Cash.Value -= Price
				end
				
				local itemToUnlockId = Button:GetAttribute("IdOfItemToUnlock")
				if not itemToUnlockId then warn("You forgot the ID bruh") return end
				
				for _, item in TemplateItems:GetChildren() do
					if item:GetAttribute("Id") ~= itemToUnlockId then continue end
					local itemClone = item:Clone()
					local itemCFrame
					
					if itemClone:IsA("Model") then
						itemCFrame = itemClone:GetPivot()
					elseif itemClone:IsA("BasePart") then
						itemCFrame = itemClone.CFrame
					end
					
					local relativeItemCFrame = TemplatePlot.CFrame:ToObjectSpace(itemCFrame)
					local worldCFrameOfNewPlot = plot.CFrame:ToWorldSpace(relativeItemCFrame)
					if itemClone:IsA("Model") then
						itemClone:PivotTo(worldCFrameOfNewPlot)
					elseif itemClone:IsA("Basepart") then
						itemClone.CFrame = worldCFrameOfNewPlot
					end
					
					itemClone.Parent = ItemsFolder
					
					Button:Destroy()
-------------------------------------------------------------------------------------------------------------------------------------------------
					local available = true
					if available == true then
						available = false
						local IncomeIncrease = Button:GetAttribute("Income")
						local CurrentIncome = player.Income.Value
						player.Income.Value = CurrentIncome + IncomeIncrease
						print("Income Increased!")
					end
					wait (1)
					available = true
-------------------------------------------------------------------------------------------------------------------------------------------------					
				end
				
			end)
			Button.Parent = TemplateButtons
		end
		
		
		TemplateButtons.Parent = plot
		
		break
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for _, plot in Plots:GetChildren() do
		if not plot:GetAttribute("Owner") then continue end
		if plot:GetAttribute("Owner") ~= player.UserId then continue end
		plot:SetAttribute("taken", nil)
		plot:SetAttribute('Owner', nil)
		
		print("Plot has been removed from"..player.Name)
		break
	end
end)

Upon stepping on the button, it will function but sometimes it will do it the once as intended but other times it will duplicate itself X2 maybe X4 or more when called and I can’t seem to understand why. I am currently grid-locked at this point until I can figure this out but I am also pretty much an amateur at coding and trying to learn as best as I can.

Thank you for any help that can be provided, I have notifications on so I can be notified for any responses to answer as quick as I can.

2 Likes
local TemplatePlot = game.Workspace.TemplatePlot
local Plots = workspace.Plots
local PlayersDebounce = {}

game.Players.PlayerAdded:Connect(function(player)

	for _, plot in Plots:GetChildren() do
		if plot:GetAttribute("Taken") then continue end
		plot:SetAttribute("Taken", true) 
		plot:SetAttribute("Owner", player.UserId)

		print("Plot has been given to"..player.Name)

		local ItemsFolder = Instance.new("Folder")
		ItemsFolder.Name = "Items"
		ItemsFolder.Parent = plot


		local TemplateButtons = TemplatePlot.Buttons:Clone()
		local TemplateItems = TemplatePlot.Items

		for _, Button in TemplateButtons:GetChildren() do
			local RelativeCFrame = TemplatePlot.CFrame:ToObjectSpace(Button.CFrame)
			Button.CFrame = plot.CFrame:ToWorldSpace(RelativeCFrame)

			Button.Touched:Connect(function(hit)
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if not player then return end
				
				if not table.find(PlayersDebounce, player) then
					table.insert(PlayersDebounce, player) task.delay(1, function()
						table.remove(PlayersDebounce, table.find(PlayersDebounce, player))
					end)

					local IncomeIncrease = Button:GetAttribute("Income")
					local CurrentIncome = player.Income.Value
					player.Income.Value += IncomeIncrease
					print("Income Increased!")
				end
				
				if plot:GetAttribute("Owner") ~= player.UserId  then return end

				local Price = Button:GetAttribute("Price")

				if Price then
					if player.leaderstats.Cash.Value < Price then
						warn("You are a broke person")
						return
					end
					player.leaderstats.Cash.Value -= Price
				end

				local itemToUnlockId = Button:GetAttribute("IdOfItemToUnlock")
				if not itemToUnlockId then warn("You forgot the ID bruh") return end

				for _, item in TemplateItems:GetChildren() do
					if item:GetAttribute("Id") ~= itemToUnlockId then continue end
					local itemClone = item:Clone()
					local itemCFrame

					if itemClone:IsA("Model") then
						itemCFrame = itemClone:GetPivot()
					elseif itemClone:IsA("BasePart") then
						itemCFrame = itemClone.CFrame
					end

					local relativeItemCFrame = TemplatePlot.CFrame:ToObjectSpace(itemCFrame)
					local worldCFrameOfNewPlot = plot.CFrame:ToWorldSpace(relativeItemCFrame)
					if itemClone:IsA("Model") then
						itemClone:PivotTo(worldCFrameOfNewPlot)
					elseif itemClone:IsA("Basepart") then
						itemClone.CFrame = worldCFrameOfNewPlot
					end

					itemClone.Parent = ItemsFolder

					Button:Destroy()
					-------------------------------------------------------------------------------------------------------------------------------------------------
					-------------------------------------------------------------------------------------------------------------------------------------------------					
				end

			end)
			Button.Parent = TemplateButtons
		end


		TemplateButtons.Parent = plot

		break
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for _, plot in Plots:GetChildren() do
		if not plot:GetAttribute("Owner") then continue end
		if plot:GetAttribute("Owner") ~= player.UserId then continue end
		plot:SetAttribute("taken", nil)
		plot:SetAttribute('Owner', nil)

		print("Plot has been removed from"..player.Name)
		break
	end
end)
3 Likes

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