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.
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)
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.
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 :]
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