I made a sword using an animation I made. Where should I implement a script to deal damage if the animation is played. Like the classic sword, how it only does damage when the sword is swung.
Script:
local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local InputService = game:GetService("UserInputService")
local InputType = Enum.UserInputType
local animation = nil
local slashAnimation = nil
tool.Equipped:Connect(function()
animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7117685853"
slashAnimation = humanoid:LoadAnimation(animation)
end)
tool.Unequipped:Connect(function()
animation:Destroy()
slashAnimation = nil
end)
local debounce = false
InputService.InputBegan:Connect(function(input, processed)
if input.UserInputType == InputType.MouseButton1 and slashAnimation and not processed then
if debounce then return end
debounce = true
slashAnimation:Play()
slashAnimation.Stopped:Wait()
debounce = false
end
end)
Thanks,
Also what should the script be, like should I put in an if statement that has like a touch function that deals damage when the animation plays?
I’m not 100 percent if this would work since I’m not a great scripter nor have I tested this but,
You might be able to make a slashing variable like
local slashing = false
Then within the inputservice script after the slashAnimation plays you can turn slashing = true and turn it back to false after the slashingAnimation stops.
This allows you to now have a variable of whenever it’s actually slashing and now with that you can just make a touch function and test if 1. It’s a player and 2. The slashing animation is true.
If one or neither of them are true then just return end else you would deal damage to the touched.parents humanoid.
Sorry if it’s hard to understand, typing on mobiles a struggle.
I have made something like this recently.
First of all I used a server script and tool.Activated in my script instead of InputBegan and a remote event.
What I did was a Debounce then when tool.Activated was fired I made a Handle.Touched event a couple lines down:
local swingDebounce = false
local hitDebounce = false
local dmg = 30
tool.Activated:Connect(function()
if not swingDebounce then
swingDebounce = true
wait(animationTime)
swingDebounce = false
end
end)
tool.Handle.Touched:Connect(function(hit)
if swingDebounce and not hitDebounce then
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid:TakeDamage(dmg)
end
end
end)
You can try making an invisible hitbox wrapped around the tool and trigger a .touched event when you attack to detect whether the hitbox touches a humanoid or not
Wouldn’t that damage the player though?
You could make a if then line if it does.