.Touched keeps effecting multiple players?

So I’m trying to do something with a .Touched event(?) and I’ve been having an issue. So I have a local script for which has this

for i,v in pairs(workspace.Enemies:GetChildren()) do
	v.Humanoid.Touched:Connect(function(hit)
		if deb == false then
			if hit.Parent:FindFirstChild("Humanoid") then
				print(hit)
				if not v:FindFirstChild("In Battle") then
					print("Enemy Is Not In Battle - Starting One")
					local player = players:GetPlayerFromCharacter(hit.Parent)
					if not player then return end
					print(player)
					deb = true
					startBattle(player,v) --player that started it and the enemy
				else
					
				end
			end
		end
	end)
end

Heres the startBattle function

function startBattle(player,enemy)
	local test = startBattleFunc:InvokeServer(enemy)
	if test ~= nil then
		local battleArea = test[1]
		deck = test[2]
		inhand = test[3]

		local nearestPart = nil
		local length = nil

		for c,d in pairs(battleArea:GetChildren()) do
			if d.Name == "Friendly" then
				local lengthX = player:DistanceFromCharacter(d.Position)
				if length == nil or lengthX < length then
					length = lengthX
					nearestPart = d
				end
			end
		end	

		controls:Disable()
		player.Character.Humanoid:MoveTo(nearestPart.Position)	
		spawn(function()
			wait(1.5)
			cameraTween(camera,battleArea.cam,2)
			player.Character.HumanoidRootPart.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position, enemy.HumanoidRootPart.Position)
			playerGui.ScreenGui.Frame.Battle.Visible = true						
		end)

		spawn(function()
			timer()
		end)

		cardsFolder.Parent.Deck.Text = "Cards\n"..#deck.." of "..#deck+#inhand
		for i,v in pairs(cardsFolder:GetChildren()) do
			if v.Name == "TextButton" then
				v.Name = inhand[i]
				v.Text = inhand[i]
			end
		end
	end	
end


As you can see from the vide even though 1 player touched the dummy it runs the code for the 2nd player as well. I print the player on the server and it prints both players which makes no sense because again only 1 player touched it so shouldn’t it only effect 1?

2 Likes

Looking through the first script you posted, it seems as if deb should be true the second time the enemy is touched, so startBattle should only run once.

When debugging tough situations like this, it’s useful to use Studio’s built-in debugging tools. Set a breakpoint at the if deb == false then line by clicking in the gutter:

image

Then in the script ribbon menu, use the three debugger controls to step through your code one line at a time:

image

Use the watch menu to see the values of variables at the current time of execution:

Double click the outer-most scope in the Call Stack window if you want to inspect variables outside the current scope:

This way, you can go through the script line-by-line to see exactly what goes wrong, i.e. exactly which if statement doesn’t act like you expected.

1 Like

If I’m being honest I’ve never used the debugger tools before lol so I actually have no idea what I’m looking at.

for some reason it seems to work just fine occasionally and then other times it breaks and does it for both players