Unable to detect if part did not touch something

I’m trying to make a tool that changes it’s use cooldown time if whether or not you had hit another player with it.

Hitting a player changes the cooldown to 10 seconds.
Hitting something that isn’t a player changes the cooldown to 1.75 seconds.
Hitting nothing… doesn’t change anything?

Hitting nothing is supposed change the cooldown to 1.75 seconds, though during my testing it didn’t do anything.

The solution might be very simple, I’m just starting out as a scripter.

tool.Handle.Touched:Connect(function(hit)
	if toolActive == true then
		if touchdebounce == true then return end
		touchdebounce = true
		
		if hit then
			local humanHit = hit.Parent:FindFirstChild("Humanoid")
			if humanHit then
				if humanHit.Parent == tool then return end
				print("Hit player!")
				debounceTime = 10
				particlesTime = 5
				
				humanHit:TakeDamage(50)
				stunPlayer(hit)
				wait(10)
				unstunPlayer(hit)
			elseif not humanHit then
				print("Did not hit player.")
				debounceTime = 1.75
				particlesTime = 1
			end
		else
			print("Did not hit anything.")
			debounceTime = 1.75
			particlesTime = 1
			return
		end
		
		wait(2)
		touchdebounce = false
	end
end)
Full Script
local tool = script.Parent
local toolActive = false
local debounce = false
local touchdebounce = false
local debounceTime = tool.debounceTime.Value
local particles = tool.End:FindFirstChild("electricityParticles")
local particlesTime = tool.particlesTime.Value

local anim = Instance.new("Animation")
anim.Parent = tool
anim.AnimationId = "rbxassetid://6857805039"

local function stunPlayer(target)
	if target.Parent:IsA("Model") then
		local targetChar = target.Parent
		for i, v in pairs(targetChar:GetChildren()) do
			if v:IsA("Part") then
				v.Anchored = true
			end
		end 
	end
end

local function unstunPlayer(target)
	if target.Parent:IsA("Model") then
		local targetChar = target.Parent
		for i, v in pairs(targetChar:GetChildren()) do
			if v:IsA("Part") then
				v.Anchored = false
			end
		end 
	end
end

tool.Handle.Touched:Connect(function(hit)
	if toolActive == true then
		if touchdebounce == true then return end
		touchdebounce = true
		
		if hit then
			local humanHit = hit.Parent:FindFirstChild("Humanoid")
			if humanHit then
				if humanHit.Parent == tool then return end
				print("Hit player!")
				debounceTime = 10
				particlesTime = 5
				
				humanHit:TakeDamage(50)
				stunPlayer(hit)
				wait(10)
				unstunPlayer(hit)
			elseif not humanHit then
				print("Did not hit player.")
				debounceTime = 1.75
				particlesTime = 1
			end
		else
			print("Did not hit anything.")
			debounceTime = 1.75
			particlesTime = 1
			return
		end
		
		wait(2)
		touchdebounce = false
	end
end)

tool.Activated:Connect(function()
	local character = tool.Parent
	local human = character.Humanoid
	
	if debounce == true then return end
	debounce = true
	toolActive = true
	
	local animTrack = human:LoadAnimation(anim)
	animTrack:Play()
	
	animTrack.KeyframeReached:Connect(function(keyframeName)
		if keyframeName == "startElectricity" then
			if particles then
				particles.Enabled = true
			end
		end
	end)
	
	animTrack.Stopped:Connect(function()
		wait(particlesTime)
		if particles then
			particles.Enabled = false
		end
	end)
	
	wait(debounceTime)
	debounce = false
	toolActive = false
end)

make sure if CanTouch property of the part is enabled…

The CanTouch property of the tool’s Handle is enabled, yes.

I still don’t know how to solve this problem, does anyone know a solution?

If the part didnt touch anything, then the .Touched event would never even be fired in the first place. If the tool is activated by a key or click then you should just do a check after activation to see if it touched anything, rather than the way it is now

1 Like

Ah, yea I had just realized that.

How should I check for if it had touched anything?

you could have a touched variable outside set to false, then inside the Touched event set it to true. After any touch the variable becomes true, so if it is still false after the tool finishes then it didnt touch anything

local touched = false

handle.Touched:Connect(function(part)
    touched = true
    -- do stuff
end)

if touched == false then
    --didnt hit anything
end
1 Like

It’s working so far, but when should I set it back to false again?

after you finish doing stuff if its false
which is right after of the touched == false if statement

Sorry, can you specify what “stuff” is? I included the full script in the original post.

right after this if statement should be fine
or you can also set it right after the tool is activated assuming you have tool.Activated event somewhere

1 Like