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

How can you make it so that a weapon does damage?

I have watched tons of videos and none of them explain it towards a beginner point of view. Does anyone have an easier way to understand it.

3 comments

0

Are you expecting us to give you a script? Please remember that this isn't a request site. Thank you!

0

No, I just need an explanation, sorry for any misunderstanding.

0

Alright that makes sense.

2 answers

The steps for ranged weapons are usually as follows:

  1. Weapon attacks
  2. Raycasts and checks for player in path
  3. Humanoid:TakeDamage()

Let's say I have a gun:

tool.Activated:Connect(function()
	local rayOrigin = endOfBarrel.Position
	local rayDirection = part.CFrame.LookVector * 1000
	local ray = workspace:Raycast(rayOrigin, rayDirection)

	if ray.Instance and ray.Instance.Parent and ray.Instance.Parent:FindFirstChild("Humanoid") then
		ray.Instance.Parent.Humanoid:TakeDamage(10)
	end
end

Basically it all boils down to knowing how to retrieve what you have hit. This is very different based on whether the weapon is ranged, melee, psychic or whatever.

Here's a link on raycasting which should help clear some of the confusing functions.

This is the script of a melee weapon

local tool = script.Parent
local canDamage = false
local canSwing = true

local function onTouch(otherPart)

	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")

	if not humanoid then -- this is use to find did the too touch a humanoid 
		return 
	end

	if humanoid.Parent ~= tool.Parent and canDamage then 
		humanoid:TakeDamage(35) -- here change the weapon damage
	else
		return
	end

	canDamage = false
end

local function slash()
	if canSwing then -- here is for playinf the slash animation
		canSwing = false
		local str = Instance.new("StringValue")
		str.Name = "toolanim"
		str.Value = "Slash" 
		str.Parent = tool
		canDamage = true
		wait(1) --cooldown time
		canSwing = true
	end
end

tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)

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