I am using a tool that enables this script when an attack is launched. It works fine, but when I try to add a wait() the function just stops. The script will work fine and will continue working, but the function basically stops at wait().
I tried using task.wait(), nothing. I tried firing a function that had wait in it instead of just doing wait, didn’t work.
The script:
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
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()
task.wait(1) -- Wait here
weld:Destroy() -- Destroy the weld after waiting
debounce = false -- Reset debounce here
end
end
Why do use a seperate function for the debounce/wait, wouldn’t it be smarter to do this:
local debounce = false -- define it
function touched(whoTouched)
if debounce then
return
end
debounce = true
local humanoid = whoTouched.Parent:FindFirstChildOfClass("Humanoid")
if humanoid ~= nil then
-- your logic here
wait(1) -- instead of this()
debounce = false
end
end
This still doesnt work. If it can help this script is originally disabled and is enabled with this code:
```local tool = script.Parent
local handle = tool.Handle
enabled = true
local function heal()
if enabled == false
then return end
enabled = false
local animation = script:FindFirstChild('Animation')
local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
local dance = humanoid:LoadAnimation(animation)
dance:Play()
handle.swing:Play()
tool.Stab.Disabled = false
task.wait(script.Parent.SwingDelay.Value)
tool.Stab.Disabled = true
enabled = true
end
tool.Activated:Connect(heal)
This is my script now but problem is since it doesnt wait for the function to finish it instantly destroys the weld
local 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
local function this()
task.wait(3)
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(1)
print("Damage took")
task.spawn(this)-- Wait here Destroy the weld after waiting
weld:Destroy()
debounce = false -- Reset debounce here
end
end
function Equipped()
local Character = Tool.Parent
Player = Players:getPlayerFromCharacter(Character)
end
handle.Touched:Connect(touched)