My Boss gets attacked randomly even when the player does not touch them directly

I’m unsure whats causing this as I did many checks to ensure it is the player who should be hitting it. The main idea im trying to achieve is that the stun effect will randomly play, and if its enabled and the player hits the boss, they will take damage.

local Debris = game:GetService("Debris")

local Boss = script.Parent
local Mover = Boss.Follow

local StunHighlight = Boss.StunFX

function StunFX(repeatCount)
	for i=1,repeatCount do
		local SoundClone = script.Stun:Clone()
		SoundClone.Parent = workspace.Effects
		SoundClone:Play()	

		SoundClone.Ended:Connect(function()
			SoundClone:Destroy()
		end)

		StunHighlight.Enabled = true
		task.wait(0.1)
		StunHighlight.Enabled = false
		task.wait(0.25)
	end
end

local VulnerableFrame = false

StunHighlight.Changed:Connect(function()
	if StunHighlight.Enabled == true then
		Boss.PrimaryPart.Touched:Connect(function(hit)
			local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

			local Character = hit.Parent
			local Humanoid = hit.Parent:FindFirstChild("Humanoid")

			if Player then
				if Player ~= nil then
					if Humanoid:GetState(Enum.HumanoidStateType.Freefall) then
						if not VulnerableFrame then

							local Force = Instance.new("AngularVelocity", Boss.PrimaryPart)
							Force.AngularVelocity = Vector3.new(500,50,0)
							Debris:AddItem(Force, 0.25)


							VulnerableFrame = true
							Boss.Humanoid.Health -= 25
							task.wait(5)
							VulnerableFrame = false
						end
					end
				end
			end
		end)
	end
end)

while true do
	task.wait(math.random(5,10))
	StunFX(math.random(1,3))
end
1 Like

Did you make sure whatever the boss is using to do damage to the players are not doing damage to the boss itself?

2 Likes

Im mainly working on the boss getting hit, It just likes to randomly damage itself but now that I added a part-check for the HRP. It seems to go pretty alright. But it has a random delay before the hit happens.

1 Like

I realised that I’m trying to make it so that there is a certain amount of time the player can hit the boss for, so it isn’t always vulnerable.

1 Like