whenever the player presses Q to punch it damages the player what do i do?
01 | local user = game:GetService( "UserInputService" ) |
02 | local play = game:GetService( "Players" ) |
03 | local player = game.Players.LocalPlayer |
04 | local character = player.Character |
05 | local target = player:FindFirstChild( "Humanoid" ) |
06 | local person = character:WaitForChild( "Humanoid" ) |
07 | local anim = Instance.new( "Animation" ) |
08 | local pain = true |
09 | anim.AnimationId = "rbxassetid://2642258675" |
10 | local key = Enum.KeyCode.Q |
11 | local function IsQkeydown() |
12 | return user:IsKeyDown(key) |
13 | end |
14 |
15 |
My question is... How on Earth did any of that code even cause damage!?
You're trying to define the Humanoid through the LocalPlayer, this severely bewilders me!
But, in some twisted universe, you managed to get it get the humanoid of the very player, that's why it's hurting the player.
I recommend you remove the "if" from:
1 | if target.Touched:Connect( function (hit) |
would be:
1 | target.Touched:Connect( function (hit) |
As GoldAngelInDisguise was hinting towards. I recommend creating a hit function like this (and defining Humanoid properly!):
01 | local hmd = character:WaitForChild( "Humanoid" , 5 ) |
02 |
03 | local db = false -- debounce (prevents the function from being fired a million times |
04 | hmd.Touched:connect( function (hit) |
05 | if db = = false then |
06 | db = true |
07 | if hit.Parent:IsA( "Model" ) then -- Checks if target is a model |
08 | if hit.Parent:FindFirstChildOfClass( "Humanoid" ) then -- Checks if the hit object has a humanoid |
09 | target = hit.Parent:FindFirstChildOfClass( "Humanoid" ) |
10 | target:TakeDamage( 5 ) |
11 | end |
12 | end |
13 | end |
14 | wait( 0.4 ) |
15 | db = false |
16 | end ) |
Just how your script didn't even stop working confuses me to the deepest parts of my brain. But I'll just proclaim you're using black Roblox magic and give you that script excerpt to help you during your journey.