My tycoon upgraders are acting weird

So basically I was playtesting my game to test lag spikes (They gone now) But well, uh, for some reason my upgraders are upgrading blocks TONS of times.
Here’s the code I use for the upgraders:

local upgradePart = script.Parent:WaitForChild("UpgradePart")
upgradePart.Touched:Connect(function(hit)
	if hit:GetAttribute("Value") then
		if hit:GetAttribute("HasBeenAdvancedUpgraded") then return end
		hit:SetAttribute("HasBeenAdvancedUpgraded")
		local value = hit:GetAttribute("Value")
		local newValue = value * script.Parent:GetAttribute("UpgradeValue")
		hit:SetAttribute("Value", newValue)
	end
end)

For some reason the bug always happened right after I recorded a vid but here’s the problem
Sometimes they sell for more than the normal price 30. Once, one of them sold for like 1735272 cash or something. I want to fix this quickly as it could easily limit my ability to test the game. Does anyone know what’s wrong with my code?

Maybe implementing a debounce would fix your problem maybe using :GetServerTimeNow()?

local DEBOUNCE_TIME = 1

local upgradePart = script.Parent:WaitForChild("UpgradePart")
upgradePart.Touched:Connect(function(hit)
	local lastTime = workspace:GetServerTimeNow()
	
	if hit:GetAttribute("Value") then
		if hit:GetAttribute("HasBeenAdvancedUpgraded") then return end
		hit:SetAttribute("HasBeenAdvancedUpgraded")
		
		if (workspace:GetServerTimeNow() - lastTime) >= DEBOUNCE_TIME then
			lastTime = workspace:GetServerTimeNow()
			
			local value = hit:GetAttribute("Value")
			local newValue = value * script.Parent:GetAttribute("UpgradeValue")
			hit:SetAttribute("Value", newValue)
		end
	end
end)

If I use this script, the upgraders simply won’t upgrade.

hit:SetAttribute("HasBeenAdvancedUpgraded")

You forgot to pass in true.

1 Like

This also made me realize I need to make some balance changes in my game :skull:

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