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

I need someone to explain to me how to make a punch deal damage to another player?

Asked by 4 years ago

I am a very new Roblox scripter and have absolutely no idea what I am doing. I am trying to create a punch script. I have a local script inside of StarterCharacterScripts that plays the punch animation and fires a remote event to do the damage, but i have no idea how to actually deal the damage. I have looked at the Roblox wiki, this website, and YouTube tutorials, but nothing has helped. I have tried to modify other's scripts, but to no avail. I would prefer if someone would explain what I need to do, rather than giving me a script to copy and paste, as I actually want to learn how to script, rather than copy scripts.

Here is the LocalScript:

 local player = game.Players.LocalPlayer

repeat wait() until player.Character.Humanoid
local playerboy = player.Character.Humanoid
local mouse = player:GetMouse()
local Humanoid = player.Character.Humanoid
local damage = 200
local hit = true



game:GetService("UserInputService").InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
    local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=4800165198"


   local playAnim = playerboy:LoadAnimation(anim)
   playAnim:Play()

game.ReplicatedStorage.PunchEvent:FireServer()
end
end) 

and here is the script in ServerScriptStorage:



game.ReplicatedStorage.PunchEvent.OnServerEvent:Connect(function(player) -- the first argument is always the player who fired the remote print("yes") local LeftHand = player.Character.Humanoid script.Parent.LeftHand.Touched:connect(function(hit, dmg) if hit.Parent.Humanoid and dmg == true then hit.Parent.Humanoid:TakeDamage(200) -- change 20 to amount of damage dmg = false wait(1) dmg = true end end) end)

Thanks in advance for helping a noob out :).

0
The problem I think is your variable for LeftHand, Instead of "player.Character.Humanoid" why not "player.Character.HumanoidRootPart" or the lefthand itself. CjayPlyz 643 — 4y
0
I'm not sure but the humanoid I think doesn't have an hitbox making it imposibble to touch CjayPlyz 643 — 4y
0
How would I make a hitbox? I am getting the error "LeftHand is not a valid member of ServerScriptService" chimpyochimp 0 — 4y
0
script.Parent.LeftHand -- you were calling the Parent of the script, which is ServerScriptStorage, and then asking it for an instance named "LeftHand", which does not make any sense. akatak_prime 48 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

One quick thing to note: - The "Humanoid" instance is not present in the player instance in game.Players, but in the player instance in game.Workspace

I will start by deleting unused or repeated variables. These will be kept:

local player = game.Players.LocalPlayer
local playerboy = player.Character.Humanoid

Now, going back to my important note, I will change your playerboy variable to instead search game.Workspace for your player.

local playerboy = game.Workspace:WaitForChild(player.Name)

:WaitForChild("child") is used to wait for an object to load before proceeding. The player instance in game.Workspace is named the player's username, so we will search based on the name of the player. Now I will make another variable accessing the Humanoid, once you've found the player in game.Workspace:

local playerHumanoid = playerboy:WaitForChild("Humanoid)

On to your code:

game:GetService("UserInputService").InputBegan:connect(function(input)
        if input.KeyCode == Enum.KeyCode.Q then
            local anim = Instance.new("Animation")
        anim.AnimationId = "http://www.roblox.com/asset/?id=4800165198"

        local playAnim = playerboy:LoadAnimation(anim)
        playAnim:Play()

        game.ReplicatedStorage.PunchEvent:FireServer()
    end
end) 

First of all, please make sure to indent all your lines correctly. I indented them for you. Moving on, we are going to change the animation variables as needed.

local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=4800165198"

local playAnim = playerHumanoid:LoadAnimation(anim)
playAnim.Looped = false --to make sure the animation doesn't play over and over again.

playAnim:Play()

Your :FireServer() seems to be correct, so let's move on to the server script. First of all, I'm going to correctly indent this for you:

game.ReplicatedStorage.PunchEvent.OnServerEvent:Connect(function(player) -- the first argument is always the player who fired the remote
    print("yes")
    local LeftHand = player.Character.Humanoid

    script.Parent.LeftHand.Touched:connect(function(hit, dmg)
        if hit.Parent.Humanoid and dmg == true then
            hit.Parent.Humanoid:TakeDamage(200) -- change 20 to amount of damage
                dmg = false
                wait(1)
                dmg = true

        end
    end)
end)

Your initial server event function looks good to me. First of all, we are going to change your variable, once again, to search for the game.Workspace player, not for the game.Players player. I'm going to rename your variable to make it less confusing for me.

local LeftHand = player.Character.Humanoid

turns into

local playerCharacter = game.Workspace:WaitForChild(player.Name)
local playerHumanoid = playerCharacter:WaitForChild("Humanoid")

Now, let's rewrite your function. You are using the Humanoid.Touched event, which has two parameters: the object that touched the Humanoid, and the limb that is doing the touching.

playerHumanoid.Touched:Connect(function(object, limb)

First, we want to make sure that the limb is the left hand, so we'll add an if statement:

playerHumanoid.Touched:Connect(function(object, limb)
    if limb.Name == "LeftHand" then

    end
end)

Next, we want to make sure that the object being touched is a player part. We'll add another if statement checking that the object's parent has a Humanoid:

playerHumanoid.Touched:Connect(function(object, limb)
    if limb.Name == "LeftHand" then
        if object.Parent.Humanoid then
            object.Parent.Humanoid:TakeDamage(200)
        end
    end
end)

I think that's it for now. However, I will warn you that I haven't tested this code at all, and that there are likely bugs. If you can't figure out how to fix something that I wrote, or you would like me to clarify something I said, please tag me or contact me in some manner, and I'll get back to you as soon as I can. In the meantime, I'm gonna eat lunch.

Ad

Answer this question