I KNOW KEYDOWN IS DEPRECATED
Hi, I made an animation where I give a punch. Now, I'd like to give damage when a player or an humanoid get's hit by it. I tried this, not working though.
local player = game.Players.LocalPlayer repeat wait() until player.Character.Humanoid local humanoid = player.Character.Humanoid local mouse = player:GetMouse() local db = false local anim = Instance.new("Animation") local damage = game.ReplicatedStorage.Damage.Value anim.AnimationId = "http://www.roblox.com/asset/?id=2055511545" -- id of animation mouse.KeyDown:connect(function(key) if db == false then if key == "q" then db = true local playAnim = humanoid:LoadAnimation(anim) playAnim:Play() wait(1) db = false game.Players.LocalPlayer.Character.RightHand.Touched:Connect(function(plr) local huma = plr:WaitForChild("Humanoid") if huma then huma.Health = huma.Health - damage end end) end end end)
The problem is that the passed variable "plr" is the part that touched your right hand. That means that you have to search for the humanoid in the plr's parent, not in plr.
local player = game.Players.LocalPlayer repeat wait() until player.Character.Humanoid local humanoid = player.Character.Humanoid local mouse = player:GetMouse() local db = false local anim = Instance.new("Animation") local damage = game.ReplicatedStorage.Damage.Value anim.AnimationId = "http://www.roblox.com/asset/?id=2055511545" -- id of animation mouse.KeyDown:connect(function(key) if db == false then if key == "q" then db = true local playAnim = humanoid:LoadAnimation(anim) playAnim:Play() wait(1) db = false game.Players.LocalPlayer.Character.RightHand.Touched:Connect(function(part) local huma = part.Parent:FindFirstChild("Humanoid") if huma then huma.Health = huma.Health - damage end end) end end end)
also i would recommend to use :TakeDamage() function on the humanoid so people can't kill players with forcefields etc., but it's up to you.
KeyDown
, use UserInputService
or ContextActionService
.-- LocalScript local plr = game:GetService("Players").LocalPlayer local char = plr.Character or plr.CharacterAdded:Wait() local dmg = 10 -- set to your damage function punch() if not char:FindFirstChild'PunchAnim' then local anim = Instance.new'Animation' anim.Name = 'PunchAnim' anim.AnimationId = '' -- put your ID here anim.Parent = char end local dmgScript = script.DmgScript:Clone() dmgScript.Parent = char.RightHand dmgScript.Disabled = false wait(0.5) dmgScript:Destroy() end game:GetService("ContextActionService"):BindAction( "Punch", punch, false, Enum.KeyCode.Q )
-- Server Script local dmg = 10 script.Parent.Touched:Connect(function(part) if part.Parent:FindFirstChild"Humanoid" then if part.Parent ~= script.Parent.Parent then part.Parent.Humanoid:TakeDamage(dmg) end end end)
just take a sword script, make the mesh invisible and replace the animations, and change the range and/or damage if you need to