Detect Hit with userinput service

Hello, i made this script that if you click, it plays and animation. But i have a custom StarterCharacter model with a Stick part in it, how do i make it that if someone touches the stick part when clicked, it deals them damage and makes adds one to a leaderstats value


local UserInputService = game:GetService("UserInputService")
local Animation = script.Stick
local debounce = false


local function onInputBegan(input, _gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and debounce == false then
		debounce = true
		print("The left mouse button has been pressed!")
		local Player = game. Players. LocalPlayer
		local Character = Player. Character
		local Humanoid = Character :WaitForChild ("Humanoid")
		local Anim = Humanoid: LoadAnimation (Animation)
		Anim:Play()
		wait(0.8)
		debounce = false
	end
end

UserInputService.InputBegan:Connect(onInputBegan)

Can someone help me there ? I really need this

Use stick.Touched. In the touched function, check if debounce is true, so it only works when you click.
Also clean up your code. Lot of weird spaces.

how do i get stick from the player?

use the LocalPlayer.Character object. So it would be LocalPlayer.Character.Stick.

how do i detect if an player (not the one who clicks) get hit by the stick then

Do you want the full code or just the logic behind it?

The fullcode would be nice since im a beginner at scripting

local stick = game.Players.LocalPlayer.Character.Stick
local damage = 10

stick.Touched:Connect(function(hit)
    if hit.Parent =~ stick.Parent and debounce == false then
        local h = hit.Parent:FindFirstChildOfClass("Humanoid")
        if h then
            h.Value -= damage
        end
    end
end)

Im giving the full thing as you’re a beginner. You should always avoid copy pasting and try writing yourself using the logic as that’s better to learn.

I understand , where should i put it in my script?

LocalScripts should be placed in characters or ui. So put it in the tool itself

You should be using the animator of the humanoid to load animations.

        local Animator: Animator = Character:WaitForChild("Humanoid"):WaitForChild("Animator")
		local Anim = Animator:LoadAnimation (Animation)
		Anim:Play()

If you are looking to create an hitbox system, you might wanna have the part where the damage is applied handled on the server to avoid future issues and possible exploits.

If you kill an humanoid on your client only, it could cause potential issues such as a desynchronisation between what you see and what they see.

its not a tool, its a local script in starter gui

What do you mean? I dont understand

@isaagomendabuilder did you try writing off my script?

He’s a newbie so he basically is going to have 0 idea of what youre speaking.

Doesn’t matter if its a tool or a local script, humanoid:loadanimation() never works, because roblox already deprecated it…

If you kill an humanoid through a Local Script, it may cause some issues in the future. Especially on a PVP/PVE game.

In that case, fire a Remote Event (RemoteEvent | Roblox Creator Documentation) that will send a message to the Server that he can intercept (RemoteEvent | Roblox Creator Documentation) to validate the fact there was a direct hit and apply the damage correctly, to avoid desynchronisation.

If a feature is mentioned as deprecated, it doesn’t necessarily means it’s un-usable.

I just tested on a baseplate and LoadAnimation() still works. Deprecated only means that the feature you are using is outdated and that more efficient alternatives are available.

Humanoid:LoadAnimation() does not work, I have tried to use it many times, only Animator:LoadAnimation() works…