Activated Problem Tool

Whenever the tool is activated and i wait a while then it touches a humanoid it still does the action, I want it to only do the action when the player clicks/taps.

local knife = script.Parent.Handle
local plr = script.Parent.Parent.Parent
local db = true

script.Parent.Activated:Connect(function()
	script.knifeswing:Play()
	knife.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and db == true then
			db = false
			local value = plr.folder.lastkilled
			value.Value = hit.Parent.Name

			game.ReplicatedStorage.KnifeKill:FireClient(plr)
			hit.Parent.Humanoid.Health = 0
			game.SoundService.DingSound:Play()
			script.KnifeHit:Play()
			
			task.wait(1)
			db = true
		end
	end)
end)



Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

maybe you should add like a second db like this

local knife = script.Parent.Handle
local plr = script.Parent.Parent.Parent
local db = true
local IsActivated = false

script.Parent.Activated:Connect(function()
	script.knifeswing:Play()
        IsActivated = true
       If IsActivated == true then
       IsActivated = false
	knife.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and db == true then
			db = false
			local value = plr.folder.lastkilled
			value.Value = hit.Parent.Name

			game.ReplicatedStorage.KnifeKill:FireClient(plr)
			hit.Parent.Humanoid.Health = 0
			game.SoundService.DingSound:Play()
			script.KnifeHit:Play()
			task.wait(1)
			db = true
		end
	end)
end
end)

its a little messed up

I tried it and it still does the same problem

I will try to work more on this later

1 Like

thanks here is a video of my problem btw

The reason this problem occurs is do to how you have scripted this system.
As of now after you click, anytime it touches anything it kills whatever, because .Activated doesnt end its code after a certain amount of time, thus it persists forever.

There are a couple ways to combat this, the easier way, which is just disconnecting the .Touched signal:

local knife = script.Parent.Handle
local plr = script.Parent.Parent.Parent
local db = true

script.Parent.Activated:Connect(function()
	script.knifeswing:Play()
    local Touched
	Touched = knife.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and db == true then
			Touched:Disconnect() -- The .Touched event can no longer run until we press on the screen again
			local value = plr.folder.lastkilled
			value.Value = hit.Parent.Name

			game.ReplicatedStorage.KnifeKill:FireClient(plr)
			hit.Parent.Humanoid.Health = 0
			game.SoundService.DingSound:Play()
			script.KnifeHit:Play()
			
			task.wait(1)
			db = true
		end
	end)
end)

This assumes you only want to be able to hit one person with each swing.

Practice

A little home work for you:
If you wanted to optimize this (or make it better ig) what you could do is make it so any time you swing the script sets a variable that I’m going to call “Attacking” to true, you would then wait X seconds and set it to false. you would then have the .Touched event outside of the .Activated (E.g, right after where you define the db variable)

In said .Touched event, instead of checking for db, you can check if the attacking variable is true

Thankyou for the insight but it still does the same thing but whenever it is actived whenever it is touched it does not do it again if it is touched again and it continues.

Hmm well in that case, check out the practice section I listed, that method I listed would probably work much better, and is probably more optimized.

I chnaged the script to this and used your idea:
But it still does the same thing, here is also a video shwoing it:

local knife = script.Parent.Handle
local plr = script.Parent.Parent.Parent
local db = script.Parent.db
local dbb = false


script.Parent.Activated:Connect(function()
	script.knifeswing:Play()
	db.Value = true
	local Touched
	Touched = knife.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and dbb == false then
			dbb = true
			Touched:Disconnect() -- The .Touched event can no longer run until we press on the screen again
			local value = plr.folder.lastkilled
			value.Value = hit.Parent.Name

			game.ReplicatedStorage.KnifeKill:FireClient(plr)
			hit.Parent.Humanoid.Health = 0
			game.SoundService.DingSound:Play()
			script.KnifeHit:Play()

			task.wait(1)
			dbb = false
		end
	end)
	db.Value = false
	task.wait(1)
	db.Value = true
end)

Ok what I said in the practice shouldnt include anything I said prior, from looking at your example, the .Touched shouldnt be inside of the .Activated.

Along with that there would be no reason to disconnect the .Touched event, so maybe rewrite it, if this doesnt make much sense, I can help you some more with some pointers and examples :]

1 Like

Took me awhile but I finally got it
heres the video:


the second try I clicked and it killed them

I deleted some of ur stuff so ur gonna have to add it back

script:

local knife = script.Parent.Handle
local db = true
local touchConnection

script.Parent.Activated:Connect(function()
	if db then
		db = false

		-- Disconnect any existing touch connection to prevent multiple triggers
		if touchConnection then
			touchConnection:Disconnect()
		end

		-- Connect a new touch event that only lasts for this activation
		touchConnection = knife.Touched:Connect(function(hit)
			local humanoid = hit.Parent:FindFirstChild("Humanoid")
			if humanoid then
				humanoid.Health = 0
			end
		end)

		-- Wait for a cooldown before allowing activation again
		task.wait(1)
		db = true

		-- Disconnect the touch event after the cooldown
		if touchConnection then
			touchConnection:Disconnect()
			touchConnection = nil
		end
	end
end)

I clicked on the first then waited it didn’t kill him
then on the second one I also clicked on him and killed him

Hope this helps!

1 Like

Thankyou both for the help! I created another debounce to shorten the window where you can attack

2 Likes

Nevermind. Your welcome! anymtime!

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