I use this script for a melee weapon, it activates when the tool is activated.
The script works great, except when I use a task.wait() it completely stops the function it’s in.
So yeah if anyone can help I’d be appreciated
Code:
debounce = false
Tool = script.Parent
handle = Tool.Handle
Debris = game:GetService("Debris")
Players = game:GetService("Players")
hand = Tool.Parent:FindFirstChild("RightHand")
function getPlayerFromCharacter(character)
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
if player.Character == character then
return player
end
end
end
function IsTeamMate(Player1, Player2)
return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end
function TagHumanoid(humanoid, player)
local Creator_Tag = Instance.new("ObjectValue")
Creator_Tag.Name = "creator"
Creator_Tag.Value = getPlayerFromCharacter(Tool.Parent)
Debris:AddItem(Creator_Tag, 2)
Creator_Tag.Parent = humanoid
end
function UntagHumanoid(humanoid)
for i, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
function touched(whoTouched)
local humanoid = whoTouched.Parent:FindFirstChildOfClass("Humanoid")
if humanoid ~= nil and debounce == false then
debounce = true
handle.stab:Play()
local character = whoTouched.Parent
local player = Players:getPlayerFromCharacter(character)
local Player = Players:getPlayerFromCharacter(Tool.Parent)
if player and (player == Player or IsTeamMate(Player, player) and Tool.CanFriendlyFire.Value == false)then
return end
if script.Parent.TagsPlayer.Value == true then
UntagHumanoid(humanoid)
TagHumanoid(humanoid, Player)
end
local weld = Instance.new("Weld")
weld.Parent = character
weld.Part0 = hand
weld.Part1 = character.HumanoidRootPart
humanoid:TakeDamage(40)
task.wait(1)-- Script stops working here
weld:Destroy()
end
end
function Equipped()
local Character = Tool.Parent
Player = Players:getPlayerFromCharacter(Character)
end
handle.Touched:Connect(touched)
Hey, You’re using a debounce variable to prevent the touched function from running multiple times in quick succession. After the touched function is triggered, debounce is set to true , which means any subsequent touches will be ignored until debounce is set back to false . However, I noticed that you never reset debounce back to false after the task.wait(1) line. This means that after the first touch, the touched function will never run again. To fix this, you should reset debounce after the wait
function this()
task.wait(1)
print("worked")
end
function touched(whoTouched)
local humanoid = whoTouched.Parent:FindFirstChildOfClass("Humanoid")
if humanoid ~= nil and debounce == false then
debounce = true
handle.stab:Play()
local character = whoTouched.Parent
local player = Players:getPlayerFromCharacter(character)
local Player = Players:getPlayerFromCharacter(Tool.Parent)
if player and (player == Player or IsTeamMate(Player, player) and Tool.CanFriendlyFire.Value == false)then
return end
if script.Parent.TagsPlayer.Value == true then
UntagHumanoid(humanoid)
TagHumanoid(humanoid, Player)
end
local weld = Instance.new("Weld")
weld.Parent = character
weld.Part0 = hand
weld.Part1 = character.HumanoidRootPart
humanoid:TakeDamage(40)
print("Damage took")
this()
debounce = false
end
end