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

How Do I Fix This Gun Script? It Always Does 50 Damage Even Though I Don't Want It To

Asked by 7 years ago

It's a local script inside a tool (the tool has a handle also), inside the starterpack. I made it so that if you shot someone's torso or head it would do 50 damage, and if you shot someone's legs or arms it would do 25 damage. I tested it on dummies containing humanoids that have 100 health, and my gun would always do 50 damage. Whether I shot the head, torso, arms, legs, it would always do 50 damage, no matter what! So what did I do wrong here, and how do I fix it? This is the script:

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer

tool.Equipped:connect(function(mouse)
    mouse.Button1Down:connect(function()
        local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 50)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Black")
        beam.FormFactor = "Custom"
        beam.Material = "SmoothPlastic"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

        local distance = (tool.Handle.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.3, 0.3, distance)
        beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

        game:GetService("Debris"):AddItem(beam, 0.1)

        if part then
            local humanoid = part.Parent:FindFirstChild("Humanoid")

            if not humanoid then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            end

            if humanoid.Torso or humanoid.Head then
                humanoid:TakeDamage(50)
            elseif humanoid.RightArm or humanoid.LeftArm or humanoid.RightLeg or humanoid.LeftLeg then
                humanoid:TakeDamage(25)
            end
        end
    end)
end)
0
line 32: humanoid:TakeDamage(100) -- not 50 tonyv537 95 — 7y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago

The character's limbs do not reside in the Humanoid, they are directly in the Character model.

Also, you have to actually compare 'part' to the limbs, otherwise you're just checking if those limbs exist or not.

local tor,head = part.Parent.Torso,part.Parent.Head;
local rarm,larm = part.Parent.RightArm,part.Parent.LeftArm;
local rleg,lleg = part.Parent.RightLeg,part.Parent.LeftLeg;

if part == tor or part == head then
    humanoid:TakeDamage(50)
elseif part == rarm or part == larm or part == rleg or part == lleg then
    humanoid:TakeDamage(25)
end
1
Too add to that make sure to account for if the user is R15 or not. plasma_node 343 — 7y
0
After I changed the script according to the above and checking for any errors, the local tool variable was underlined, and the gun does not shoot anymore. OzzieOrange -14 — 7y
Ad

Answer this question