Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Help my punch script isnt working and it makes me take damage how do i fix this?

Asked by 5 years ago

whenever the player presses Q to punch it damages the player what do i do?


local user = game:GetService("UserInputService") local play = game:GetService("Players") local player = game.Players.LocalPlayer local character = player.Character local target = player:FindFirstChild("Humanoid") local person = character:WaitForChild("Humanoid") local anim = Instance.new("Animation") local pain = true anim.AnimationId = "rbxassetid://2642258675" local key = Enum.KeyCode.Q local function IsQkeydown() return user:IsKeyDown(key) end local function Input(input, gameProcessedEvent) if not IsQkeydown() then else print("hit") local AnimationTrack = person:LoadAnimation (anim) AnimationTrack:Play() if target.Touched:Connect(function(hit) target:TakeDamage(5) end) then end end end user.InputBegan:Connect(Input)
0
line 23: target.Touched is an event, it does not return a bool but returns a signal where you can connect function listeners GoldAngelInDisguise 297 — 5y
0
How do i do that? EzireBlueFlame 14 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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:

if target.Touched:Connect(function(hit)

would be:

target.Touched:Connect(function(hit)

As GoldAngelInDisguise was hinting towards. I recommend creating a hit function like this (and defining Humanoid properly!):

local hmd = character:WaitForChild("Humanoid",5)

local db = false -- debounce (prevents the function from being fired a million times
hmd.Touched:connect(function(hit)
    if db == false then
        db = true
        if hit.Parent:IsA("Model") then -- Checks if target is a model
            if hit.Parent:FindFirstChildOfClass("Humanoid") then -- Checks if the hit object has a humanoid
                target = hit.Parent:FindFirstChildOfClass("Humanoid")
                target:TakeDamage(5)
            end
        end
    end
    wait(0.4)
    db = false
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.

1
connect is deprecated, use Connect User#24403 69 — 5y
0
wait what, I thought it was the other way around Cvieyra2test 176 — 5y
0
I thought "Connect" was the one that got deprecated lol Cvieyra2test 176 — 5y
0
It still isnt working and im still taking damage from it EzireBlueFlame 14 — 5y
Ad

Answer this question