Hello! I am trying to make a touch function but after the first time it stops. I tried a while loop and still did not work. Thank you.
local block = script.Parent
local debounce = true
local animation = block.Animation
local function hitFunction(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
local anim = humanoid:LoadAnimation(animation)
if humanoid and debounce == true then
debounce = false
local weld = Instance.new("Weld")
weld.Parent = block
weld.Part0 = block
weld.Part1 = hit.Parent:WaitForChild("HumanoidRootPart")
task.wait(4)
weld:Destroy()
end
end
block.Touched:Connect(hitFunction)
local block = script.Parent
local debounce = true
local animation = block.Animation
local function hitFunction(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
local anim = humanoid:LoadAnimation(animation)
if humanoid and debounce == true then
debounce = false
local weld = Instance.new("Weld")
weld.Parent = block
weld.Part0 = block
weld.Part1 = hit.Parent:WaitForChild("HumanoidRootPart")
task.wait(4)
weld:Destroy()
task.wait(1)
debounce = true
end
end
block.Touched:Connect(hitFunction)
local players = game:GetService("Players")
local block = script.Parent
local debounce = false
local function onTouched(hit)
if debounce then
return
end
local hitModel = hit:FindFirstAncestorOfClass("Model")
if hitModel then
local hitPlayer = players:GetPlayerFromCharacter(hitModel)
if hitPlayer then
debounce = true
local hitHRP = hitModel:FindFirstChild("HumanoidRootPart")
local weld = Instance.new("Weld")
weld.Part0 = block
weld.Part1 = hitHRP
weld.Parent = block
task.wait(4)
weld:Destroy()
debounce = false
end
end
end
block.Touched:Connect(onTouched)
Debounce fix as mentioned, you probably want to do the debounce check immediately (such that the function can be returned out of without performing any unnecessary preliminary checks). I’ve also made it so that the player’s character’s accessory handles are checked as well as its limbs (only the limbs were checked previously).