Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Press key to damage humanoid? [ANSWERED]

I have created a script that plays an animation when I press Q or E, but I also made a sword and I don't know how to make it so when I press Q or E and the sword touches a Humanoid the humanoid takes damage? So far I've gotten this, can someone explain what I've done wrong and/or maybe fix the script? Thank you !

--\ Animation Script //-- (idk if it helps or not)

--\\ VARIABLES //--

local player = game.Players.LocalPlayer or  game.Players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
local swing = Instance.new("Animation")
swing.AnimationId = "rbxassetid://04696235743"
local debounce = false
local spin = Instance.new("Animation")
spin.AnimationId = "rbxassetid://4672766085"
--\\ SWING //--

function SwingAtt(inputObject, gameProcessEvent)
if not debounce then
if inputObject.KeyCode == Enum.KeyCode.E then
debounce = true
humanoid:LoadAnimation(swing):Play()
wait(1.5) 
debounce = false
end
end
end
uis.InputBegan:Connect(SwingAtt)

--\\ SPIN //--

function SpinAtt(inputObject, gameProcessEvent)
	if not debounce then
			if inputObject.KeyCode == Enum.KeyCode.Q then
			debounce = true
			humanoid:LoadAnimation(spin):Play()
			wait(2.5) 
			debounce = false
		end
	end
end
uis.InputBegan:Connect(SpinAtt)

--\ Damage Script (need help with) //--

local swing = Enum.KeyCode.E
local spin = Enum.KeyCode.Q
local  player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local damage = 0

if player.swing then
	damage = 15
	
	script.Parent.Touched:Connect(function(part1)
		if part1.Parent:WaitForChild("Humanoid") then
			part1:TakeDamage(damage)
		end
	end)
end


if player.spin then
	damage = 30
	script.Parent.Touched:Connect(function(part2)
		if part2.Parent:WaitForChild("Humanoid") then
			part2.Parent.Humanoid:TakeDamage(damage)
		end
	end)
end

6 comments

0

Is there any errors?

0

It’s a local script you need it to be a normal script and as I don’t work with weapons/tools I can’t help

0

No error in the Output or Script Analysis, it's also a normal script inside the tool's handle

0

The take damage would need to be done in a server script. You can add a touched event on the server side then check for a playing animation on hit

0

I'm sorry but I don't know how to do that really, could you maybe help?

0

I've added the animation script if it helps.

1 answer

This might work but at the this bit

part1.Parent.Humanoid:TakeDamage(damage)

you can just type in...

part1:TakeDamage(damage)

I hope that this answers your question.

10 comments

0

I changed it then it said in the output that swing is not a valid member of Player

0

Are you using a local script?

0

Yes I am

0

Oh wait are you using game:GetService("UserInputService")?

0

OMG IM SO STUPID

0

I've totally forgot about that

0

No No thats fine I have those moments.

0

Alright so should I make it when the player presses E it changes the damage value to whatever and if a humanoid touches it it takes damage right?

0

Seems about right.

0

Alright made it work! Thanks dude!

This discussion is preserved as it was. New answers, comments, and votes are closed.