.Touched script activates even when character being near with trigger part

So, I’m working on obby game and for creating an obstacle I used spinning part which causes a damage when character touching it, but I noticed that character gains a damage even when it’s NEAR with damage part, like when it’s not even getting touched. Script located in StarterCharacterScripts. I can’t get it… Is that a engine bug? Desync? or is that a code problem? I’m also tried to trigger .Touched event by using FireServer() with localscript, result was the same. What do I do? q_q

Here’s script:

for i, bodyPart in pairs(script.Parent:GetChildren()) do
	if bodyPart:IsA('BasePart') then
		bodyPart.Touched:Connect(function(part)
			if part.Name == 'damagePart' then
				if debounce == true then
					return
				else
					script.Parent.Humanoid.Health -= 5
					debounce = true
					wait(1)
					debounce = false
				end
			elseif part.Name == 'killPart' then
				script.Parent.Humanoid.Health -= math.huge
			end
		end)
	end
end

And video with demonstration of that weird behaviour
robloxapp-20240916-1317272.wmv (1.9 MB)

2 Likes

maybe try the entire thing with a localscript? as far as i know setting your own humanoids health to 0 does replicate.

1 Like

also, your system for detecting collision with parts is very strange, although i dont think it is the problem.

its good practice to have all these parts in a table
i.e

for _, part in pairs(damageParts) do
 part.Touched:Connect(function(hit)
   local hum = hit.Parent:FindFirstChild("Humanoid")
   if hum then
      hum.Health = 0
   end
 end)
end
1 Like

I remade it into localscript and nope, it didn’t worked and in addition it triggers only once, when player gets damage/killed .Touched event doesn’t get executed anymore

where is the localscript located? it should be in startercharacterscripts i believe.

yeah, it’s located in startercharacterscripts

can i see the code of the script? or is it the same as the one you posted?

is the part spinning a mesh or union instead of classic part?
@bert_gotraxx

yeah, it’s same as in post

local debounce = false

for i, bodyPart in pairs(script.Parent:GetChildren()) do
	if bodyPart:IsA('BasePart') then
		bodyPart.Touched:Connect(function(part)
			if part.Name == 'damagePart' then
				if debounce == true then
					return
				else
					script.Parent.Humanoid.Health -= 5
					debounce = true
					wait(1)
					debounce = false
				end
			elseif part.Name == 'killPart' then
				print("DEBUG: KILL PART TOUCHED")
				script.Parent.Humanoid.Health -= math.huge
			end
		end)
	end
end

Also, in localscript I’m tried to refer to character with game.Players.LocalPlayer instead refering to it by using script.Parent

local debounce = false

for i, bodyPart in pairs(game.Players.LocalPlayer.Character:GetChildren()) do
	if bodyPart:IsA('BasePart') then
		bodyPart.Touched:Connect(function(part)
			if part.Name == 'damagePart' then
				if debounce == true then
					return
				else
					game.Players.LocalPlayer.Character.Humanoid.Health -= 5
					debounce = true
					wait(1)
					debounce = false
				end
			elseif part.Name == 'killPart' then
				print("DEBUG: KILL PART TOUCHED")
				game.Players.LocalPlayer.Character.Humanoid.Health -= math.huge
			end
		end)
	end
end

both of these variants are doesn’t work

it’s the regular part, BasePart not a union and not a mesh

that’s a good idea, but there’s like A LOT of them, won’t it be too much parts to keep track of? Like, it won’t cause a lags?

i dont suspect lag will be much different between the two. the one i provided is common practise and should theoretically work better.

here, i revised your script using that principle. untested, but should work

local char = script.Parent -- this is fine
local hum = char:FindFirstChild("Humanoid")
local parts = workspace:GetChildren() -- or wherever your killparts are
local db = false



for _, part in pairs(parts) do
	part.Touched:Connect(function(hit)
		if hit.Parent ~= char then return nil end
		if part.Name == "damagePart" then
			if db == true then return nil end
			db = true
			hum.Health -= 5
			wait(1)
			db = false
		elseif part.Name == "killPart" then
			hum.Health = 0 
		end
	end)
end

whoops, forgot to add a touched event. ill edit it in a second

here:

local char = script.Parent -- this is fine
local hum = char:FindFirstChild("Humanoid")
local parts = workspace:GetChildren() -- or wherever your killparts are
local db = false



for _, part in pairs(parts) do
	part.Touched:Connect(function(hit)
		if hit.Parent ~= char then return nil end
		if part.Name == "damagePart" then
			if db == true then return nil end
			db = true
			hum.Health -= 5
			wait(1)
			db = false
		elseif part.Name == "killPart" then
			hum.Health = 0 
		end
	end)
end

have you tried this in studio from the server side to check for any visual delays

also check debounce before you give other instructions

I tried, character still gets a damage even when it’s about to touch damage part

how exactly are you rotating the part

I’m tried to spectate on player from server side, yes it actually has delay on client side, but I can’t get it why? and how to fix it?

with CylindricalConstraint, no scripts