Part of the script stops working properly for no aparent reason

Hey, so i have this script for a module that handles some map spawning and allocates some systems in it. So i have this thing that when you step on a pressure plate, it opens a glass door so you can pick a key inside of it, when you touch the key it clones it from the storage and gives it to the player. The thing is it works until it doesnt, for some reason it randomly breaks forever and only gets fixed when the instance/server is reset. Like the system keeps working except for the part when you touch the key the player no longer gets anything.

Here is the script where i think the issue could be that makes it stop working but i dont see it.

-- Binds connections to the various parts of the map
function MapFunctions.Load(Map: Model, Curse: Player): nil
	Doors = Map.Scripted:FindFirstChild("Doors")
	Key = Map.Scripted:FindFirstChild("Key")
	Pistols = Map.Scripted:FindFirstChild("Pistols")
	Pressure_Plate = Map.Scripted:FindFirstChild("Pressure Plate")
	
	
	if Doors then
		local DoorInstances: {Instance} = Doors:GetChildren()
		for i = 1, #DoorInstances do
			local Door: MeshPart = DoorInstances[i]
			local onTouched; onTouched = Door.Touched:Connect(function(Hit: Part)
				if Hit:GetAttribute("IsKey") then
					OpenDoor(Door, 5)
					RemoveConnection(onTouched)
				end
			end)
			AddConnection(onTouched)
		end
	else
		warn("Doors folder not found in map")
	end
	
	if Key then
		local Key: Part = Key["Key Holder"].Key
		local TeleportPart: Part = Key.Parent.Hitbox
		local TeleportReceive: Part = Key.Parent.TP
		local Button: Part = Key.Button.Value.Button
		local ButtonStander: Player = Button.Stander
		
		local Touched = TeleportPart.Touched:Connect(function(Hit: Part)
			local HitPlayer: Player = Players:GetPlayerFromCharacter(Hit.Parent)
			local HitCharacter: Model = Hit.Parent
			if HitPlayer and HitCharacter then
				HitCharacter.PrimaryPart.CFrame = TeleportReceive.CFrame + Vector3.new(math.random(-TeleportReceive.Size.X / 2, TeleportReceive.Size.X / 2), 0, math.random(-TeleportReceive.Size.Z / 2, TeleportReceive.Size.Z / 2))
			end
		end)
		
		local Connection; Connection = Key.Touched:Connect(function(Hit: Part)
			local HitPlayer: Player = Players:GetPlayerFromCharacter(Hit.Parent)
			if HitPlayer and HitPlayer ~= ButtonStander.Value and Key:GetAttribute("Enabled") == true and HitPlayer ~= Curse and not PlayersWithKeys[HitPlayer] then
				local ReplicatedKey = Server.Tools.Key:Clone()
				ReplicatedKey.Parent = HitPlayer.Backpack
				PlayersWithKeys[HitPlayer] = true
			end
		end)
		AddConnection(Connection)
	else
		warn("Key folder not found in map")
	end
	
	if Pistols then
		local Folders: {Instance} = Pistols:GetChildren()
		for i = 1, #Folders do
			local PistolModel: Model = Folders[i].Pistol
			local PistolParts: {Instance} = PistolModel.Pistol:GetChildren()
			local Platform: Part = Folders[i].Platform
			local Touched = Platform.Touched:Connect(function(Hit: Part)
				local HitPlayer: Player = Players:GetPlayerFromCharacter(Hit.Parent)
				if HitPlayer and HitPlayer ~= Curse and not PlayersWithGuns[HitPlayer] then
					local GunTool: Tool = Server.Tools.Pistol:Clone()
					GunTool.Parent = HitPlayer.Backpack
					PlayersWithGuns[HitPlayer] = true
				end
			end)
			AddConnection(Touched)
		end
	else
		warn("Pistols folder not found in map")
	end
	
	if Pressure_Plate then
		local Button: Part = Pressure_Plate["Pressure Plate"].Button
		local ButtonHitbox: Part = Pressure_Plate["Pressure Plate"].ButtonHitbox
		local Stander: Part = Button.Stander
		local Key: Model = Button.Key
		local Touched = ButtonHitbox.Touched:Connect(function(Hit: Part)
			local StandingPlayer = Players:GetPlayerFromCharacter(Hit.Parent)
			if StandingPlayer and StandingPlayer ~= Curse and not Button:GetAttribute("Down") then
				Button:SetAttribute("Down", true)
				Button.Position = Button.Position - Vector3.new(0, 1 / 2, 0)
				Stander.Value = StandingPlayer
				Key.Value["Glass"].Transparency = 0.9
				Key.Value["Glass"].CanCollide = false
				Key.Value.Key:SetAttribute("Enabled", true)
			end
		end)
		local TouchEnded = ButtonHitbox.TouchEnded:Connect(function(Hit: Part)
			local StandingPlayer = Players:GetPlayerFromCharacter(Hit.Parent)
			if StandingPlayer and StandingPlayer == Stander.Value and StandingPlayer ~= Curse and Button:GetAttribute("Down") then
				Button:SetAttribute("Down", false)
				Button.Position = Button.Position + Vector3.new(0, 1 / 2, 0)
				Stander.Value = nil
				Key.Value["Glass"].Transparency = 0
				Key.Value["Glass"].CanCollide = true
				Key.Value.Key:SetAttribute("Enabled", false)
				local TouchingParts = Key.Value["Glass"]:GetTouchingParts()
				for i = 1, #TouchingParts do
					local Part = TouchingParts[i]
					if Part.Parent:FindFirstChildOfClass("Humanoid") and Part.Parent:IsA("Model") then
						Part.Parent:MoveTo(Key.Value["Glass"].Position)
					end
				end
			end
		end)
		AddConnection(Touched)
		AddConnection(TouchEnded)
	else
		warn("Pressure Plate folder not found in map")
	end
end

function MapFunctions.Clear()
	ClearConnections()
	PlayersWithGuns = {}
end

return MapFunctions

Any help is appreciated. Thank you very much.

Just if case, im still trying find a way to fix this bug

Still looking for solution to try tomorrow

Still looking, im working on other stuff while i wait if someone knows

Is it only occurring with players who’ve had a key before? If so, check if you’re cleaning PlayersWithKeys properly.
Is it otherwise occurring with all players? If so, ensure your “Enabled” attribute is being set properly.

Otherwise… I have no idea, lol.

Edit: Also, the reason you can’t see it, is because we can’t either.
From what I can see, that’s a logically sound script with no immediate flaws.

1 Like

I did some adjusting taking in consideration what you mentioned about cleaning PlayersWithKeys given that the game works fine the first couple games and breaks after a while, so im asumming thats where the issue could be. So far everything is working fine with the new adjustments, i will test for some days and if it doesnt break anymore i will mark your reply as solution. Thank you very much!