WHY DOES ? ? collectionservice add multiple tools every round?

  1. What do you want to achieve?
    I want to find everything in the game tagged ‘cat’ and iterate through all its parts to see if they’ve touched the ‘mouse’ (this is part of a tool). when they’ve touched the mouse, the player who has the mouse dies, the tool is destroyed, a new mouseplayer is randomly selected and given a new mouse tool. any cat can once again ‘catch’ the mouse by touching it.

  2. What is the issue?
    I’m using collectionservice to find the cats and check whether they’ve touched the mouse. I try to disconnect the touched and break the for loops that check whether the cats touched anything with a variable (mousecaught) when the mouse has been touched. Then when the mousecharacter respawns in, we start checking again, which means the char can die again by having the mouse touched.

PROBLEM: everytime the mouse is touched and the mouseplayer spawns in again, theres another tool. 1 tool, 2 tool, 3 tools, 5 tools. its like with every round a synchronous process kicks off and the whole script is run once every round, twice every round, three times every round, four times etc…
I DONT UNDERSTAND WHY. i am basically at my wit’s end…

  1. What solutions have you tried so far?

I looked around. i thought it was events. took out events and put it all in one script. i put in ifstatements and for loops breaks. i just don’t understand what da heck is going on. please help me, anyone

--get services
local CollectionService = game:GetService("CollectionService")
local mouseplayer

local ServerScriptService = game:GetService("ServerScriptService")
local playerservice = game:GetService("Players")
playerservice.PlayerAdded:Wait() -- this makes the script wait until a player has been added!!!!!

local players = playerservice:GetPlayers()


--variables
local connection
local mousecaught = false

local RandomPlayer 
local mouseplayer
local newInstrument
local mouseplayerknown

-- FUNCTIONS

local function getNewMousePlayer()
	RandomPlayer = players[math.random(1, #players)] -- random number between 1 and amount of players
	mouseplayer = RandomPlayer
	mouseplayerknown = true
end

local function setInstrumentToMouseplayer ()
	local mouseplayerchar = mouseplayer.Character or mouseplayer.CharacterAdded:Wait()
	local instrument = game.ServerStorage["The Instrument"]
	newInstrument = instrument:Clone()
	newInstrument.Parent = mouseplayer.Backpack -- put tool in backpack
	mouseplayerchar.Humanoid:EquipTool(newInstrument) -- equip tool to mouseplayer
end

local function startRound()
	mouseplayer.CharacterRemoving:Connect(function() -- when mouseplayerchar despawns
		getNewMousePlayer()
		setInstrumentToMouseplayer()
	end)
	
	mouseplayer.CharacterAdded:Connect(function() -- when mouseplayerchar respawns 
	mousecaught = false
	print("hello yeah")
	end)
end

local function detectMouseCatch()
	
	if mousecaught then return
	else
		print(mousecaught)
		for _, cat in CollectionService:GetTagged("cat") do -- everything tagged 'cat' runs this
			if mousecaught == true then -- IF the mouse has been caught
				break --stop
				else
				for _, part in ipairs(cat:GetChildren()) do -- for every part in a cat
					if mousecaught == true then -- IF the mouse has been caught
						break --stop
					else
						if part:IsA("BasePart") then --for every verified child that is a part
							if mousecaught == true then -- IF the mouse has been caught
								break --stop
							else -- otherwise go on:
								connection = part.Touched:Connect(function(hit) --connect to the regularly checking 'touched' function
									print(mousecaught)
									if mousecaught == true then -- if the mouse has been caught
										return -- stop
									else -- otherwise:
										if hit.Parent.Name == "Mouse" then -- then when the cat's part touched something see if parent is mouse
											connection:Disconnect() -- if so, disconnect from touched event so this part is registered only once
											local mouse = hit.Parent -- the parent then is the mouse
											mousecaught = true -- the mouse has been caught!
											print(hit)
											
											newInstrument:Destroy() -- the instrument is destroyed in mouseplayer						
											mouseplayer.Character.Humanoid.Health = 0 --set mouse player health to zero
											startRound()

											for _, mousepart in ipairs(mouse:GetChildren()) do -- for every obj in the mouse
												if mousepart.Name == "MiddlePart" then
													-- was supposed to move mouse to mouth of cat but cannot set position on part of tool

												end
												if mousepart.ClassName == "Part" then -- if its a part
													mousepart.Anchored = true -- anchor the parts

												end
											end	

											-- add CAUGHT gui (BY playername!!/YOU)
											-- add CAUGHT sound (bite sound, TJING)
											if cat.ClassName == "Player" then
												-- get points
											end
										end

									end
								end)
							end
						end
					end	
				end
			end
		end
	end
end
-- Do above functions at start of game
getNewMousePlayer()
setInstrumentToMouseplayer()
detectMouseCatch()

-- learned from this that detectmouscatch need not be called by anything, setting the mousecaught variable to false is enough...

I solved it.

it wasnt collectionservice. it was the STUPID EVENTS character removing and character added.

ALWAYS disconnect events. or they just keep going and mess everything up.


local function startRound()
	print("how many times repeat?")
	listenformouseplayerdespawn = mouseplayer.CharacterRemoving:Connect(function() -- when mouseplayerchar despawns
		getNewMousePlayer()
		setInstrumentToMouseplayer()
		print("how many times repeat?")
		listenformouseplayerrespawn:Disconnect()
	end)
	
	
	listenformouseplayerrespawn = mouseplayer.CharacterAdded:Connect(function() -- when mouseplayerchar respawns 
	mousecaught = false
	print("hello yeah")
	listenformouseplayerdespawn:Disconnect()
	end)
end

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