TimerAttachment is duplicating everytime a new player joins

  1. What do you want to achieve?
    I want to remove the old timer/attachment of a base every time a new player leave or join the game. So that a new timer will start to countdown on that timer/attachment.

  2. What is the issue?


    The timer/attachment duplicates every time a new player join the game while the timer is running or not. And the old timer/attachment doesn’t get remove if they leave.

  3. What solutions have you tried so far?
    I’ve already set a code to remove the timer attachment in ResetBase:

local TimerAttachment = Base.Panel.ControlPanel.Timer:FindFirstChild("TimerAttachment")
if TimerAttachment then TimerAttachment:Destroy() end

But it only works once. Like if a player A assign to that new fresh base, the timer/attachment will get remove and if a new player B join that base, it will not duplicate but if that player B leave the game and a new player C join that game and assign to that base, it doesn’t work anymore.

Here’s the full code for timer/lock system:

-- PLAYER RESET BASE --
local function ResetBase(Base)
	if not Base then return end

	if BaseConnections[Base] then
		for _, Connection in ipairs(BaseConnections[Base] or {}) do
			if Connection.Disconnect then
				Connection:Disconnect()
			end
		end

		BaseConnections[Base] = nil
	end

	Base.Configuration.Player.Value = nil

	for _, player in ipairs(Players:GetPlayers()) do
		local config = player:FindFirstChild("Configuration")
		if config and config:FindFirstChild("Base") and config.Base.Value == Base then
			config.Base.Value = nil
		end
	end


	for _, Door in ipairs(Base.Doors:GetChildren()) do
		Door.CanCollide = false
		Door.Transparency = 1

		for _, Descendant in ipairs(Door:GetDescendants()) do
			if Descendant:IsA("BasePart") then
				Descendant.CanCollide = false
				Descendant.Transparency = 1
			end
		end
	end

	local FriendsProximityPrompt = Base.Panel.ControlPanel.KeyboardAllow.Friends:FindFirstChild("ProximityPrompt")
	if FriendsProximityPrompt then FriendsProximityPrompt:Destroy() end

	for _, Door in ipairs(Base.Doors:GetChildren()) do
		local DoorProximityPrompt = Door:FindFirstChild("ProximityPrompt")
		if DoorProximityPrompt then DoorProximityPrompt:Destroy() end
	end

	local MultiplierAttachment = Base.CollectZone:FindFirstChild("MultiplierAttachment")
	if MultiplierAttachment then MultiplierAttachment:Destroy() end

	local CollectZoneGui = Base.CollectZone:FindFirstChild("CollectZoneGui")
	if CollectZoneGui then CollectZoneGui:Destroy() end

	local OwnerGui = Base.Owner:FindFirstChild("OwnerGui")
	if OwnerGui then OwnerGui:Destroy() end

	for _, Slot in ipairs(Base.Slots:GetChildren()) do
		local Thing = Slot.Configuration.Thing.Value

		if Thing then
			Thing:Destroy()
			Slot.Configuration.Thing.Value = nil
		end

		local CollectAttachment = Slot.Collect:FindFirstChild("CollectAttachment")
		if CollectAttachment then CollectAttachment:Destroy() end

		Slot.Configuration.Occupied.Value = false
	end
end
	-- BASE LOCK --
	local function Lock(Time)
		Locked = true

		TimerGui.Timer.Visible = true
		TimerGui.Timer.Text = tostring(Time) .. "s"
		LockorNotGui.isLock.Text = "Locked"
		buttonLock.Color = Color3.fromRGB(0, 255, 0)

		-- LOCK --
		for _, Door in ipairs(Base.Doors:GetChildren()) do
			Door.CanCollide = true
			Door.Color = Color3.fromRGB(18, 238, 212)
			Door.Transparency = 0.2

			SetProperties.Client(Player, Door, {CanCollide = false})

			for FriendPlayer in pairs(AllowedFriends) do
				if FriendPlayer:IsDescendantOf(Players) then
					SetProperties.Client(FriendPlayer, Door, {CanCollide = false})
				end
			end

			for _, Descendant in ipairs(Door:GetDescendants()) do
				if Descendant:IsA("BasePart") then
					Descendant.CanCollide = true
					Descendant.Transparency = 0.2

					SetProperties.Client(Player, Descendant, {CanCollide = false})

					for FriendPlayer in pairs(AllowedFriends) do
						if FriendPlayer:IsDescendantOf(Players) then
							SetProperties.Client(FriendPlayer, Descendant, {CanCollide = false})
						end
					end
				end
			end
		end

		for _, Door in ipairs(Base.Doors:GetChildren()) do
			local DoorProximityPrompt = Door:FindFirstChild("ProximityPrompt")
			if not DoorProximityPrompt then continue end

			SetProperties.AllClients(DoorProximityPrompt, {Enabled = true})
			SetProperties.Client(Player, DoorProximityPrompt, {Enabled = false})
		end

		if not Starter then
			NotificationEvent:FireClient(Player, string.format("Locked base for %ss!", Time), true)
		end


		task.spawn(function()
			while Time > 0 and Player and Locked and TimerGui do

				task.wait(1)

				Time -= 1

				if TimerGui and TimerGui:FindFirstChild("Timer") then
					TimerGui.Timer.Text = tostring(Time) .. "s"
				end
			end

			for _, Door in ipairs(Base.Doors:GetChildren()) do
				local DoorProximityPrompt = Door:FindFirstChild("ProximityPrompt")
				if not DoorProximityPrompt then continue end

				--for Door.Color = Color3.fromRGB(255, 170, 127)

				SetProperties.AllClients(DoorProximityPrompt, {Enabled = false})
			end

			TimerGui.Timer.Visible = true
			LockorNotGui.isLock.Text = "Unlocked"
			buttonLock.Color = Color3.fromRGB(255, 0, 0)

			for _, Door in ipairs(Base.Doors:GetChildren()) do
				Door.CanCollide = false
				Door.Transparency = 1

				for _, Descendant in ipairs(Door:GetDescendants()) do
					if Descendant:IsA("BasePart") then
						Descendant.CanCollide = false
						Descendant.Transparency = 1
					end
				end
			end

			Locked = false
		end)
	end

function Insert.Timer(Timer)
	local TimerAttachment = Instance.new("Attachment")
	TimerAttachment.Name = "TimerAttachment"
	TimerAttachment.Parent = Timer

	local TimerGui = script.TimerGui:Clone()
	TimerGui.Enabled = true
	TimerGui.Parent = TimerAttachment

	if TimerGui:IsA("SurfaceGui") then
		TimerGui.Adornee = Timer
	end

	return TimerAttachment, TimerGui
end

I do really appreciate for your any help!

use os.time() and set ResetOnSpawn to false in the properties tab

Hi, I appreciate your response!

I’m confuse about the properties tab. Do you want inside the SetProperties localscript?

No, in the gui / surface gui-----

Unfortunately, it is already set to false. Haven’t use os.time() yet but I’ll give it a try.

Any other info on why or how to fix?

Seems like you are subtracting the time by 1 which is unreliable, os.time is a better way to handle it
Documentation: