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 :).
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.