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

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

Asked by 3 years ago

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.

0
Are you expecting us to give you a script? Please remember that this isn't a request site. Thank you! Cyrus_O4 45 — 3y
0
No, I just need an explanation, sorry for any misunderstanding. GalaxyStorm86240 10 — 3y
0
Alright that makes sense. Cyrus_O4 45 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago

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.

Ad
Log in to vote
0
Answered by 3 years ago

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)

Answer this question