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

Mouse.Target.Parent not detecting headshots?

Asked by 3 years ago

I am using a hitscan system for my gun combat in my game, but when I shoot a player's head it does no damage. I can shoot the arms, legs, and torso to get damage and it works, but the head seems to not have a humanoid parent or something.


if Mouse.Target.Parent:FindFirstChild("Humanoid") then script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 15) end

That's all there is to it. Again, maybe the head isn't part of the humanoid? If anyone has had this issue please let me know what you did to fix it!

1 answer

Log in to vote
1
Answered by
TGazza 1336 Moderation Voter
3 years ago

Just created an example on how to do this, Here are my scripts:

Local Script (Inside a Handle Less Tool!)

local Tool = script.Parent --make sure this is a Tool object
local RS = game:GetService("ReplicatedStorage")
local DD = RS:WaitForChild("DealDamage",30)
function HandleDamage(Target,Damage)
    local DebugMessages = true --// flip this to false if you don't want anything printed to your output!

    --// if Target == nil or Target.Parent == nil then stop execuiting and print out the reason for the stoppage/error
    if(Target == nil) then if(DebugMessages) then warn("Error: Target is nil!") end return end
    if(Target.Parent == nil) then if(DebugMessages) then warn("Error Target.Parent is nil!") end return end

    --// OK!, This thing is not nil lets see it it has a humanoid!...
    local Humanoid = Target.Parent:FindFirstChild("Humanoid")

    if(Humanoid ~= nil) then
        if(Humanoid.Health > 0) then
            if(string.lower(Target.Name) == "head") then
                if(DebugMessages) then print("Dealing Damage[",Damage,"] to [",Target.Parent,"'s Head!]") end
                --script.Parent.DealDamage:FireServer(Target.Parent, Damage)
                DD:FireServer(Target.Parent, Damage*2) -- Two times Damage for headshots!
            else
                if(DebugMessages) then print("Dealing Damage[",Damage,"] to [",Target.Parent,"]") end
                DD:FireServer(Target.Parent, Damage)
            end
        else
    else
        if(DebugMessages) then warn("Error: Target Has no Humanoid! \n ignoring....") end
    end
end
--// the Correct way to call  this function
--// Ref
--[[
    HandleDamage(TargetObject/Model, Damage amount Delt)
]]
Tool.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
        HandleDamage(Mouse.Target, 15)
    end)
end)

Server Script:

local RS = game:GetService("ReplicatedStorage")
local DealDamageEvent = RS:WaitForChild("DealDamage",30)

local function onDealDamageEventTo(player, Target, Damage)
    if(Target == nil) then return end
    local Humanoid = Target:FindFirstChild("Humanoid")
    if(Humanoid ~= nil) then
        Humanoid:TakeDamage(Damage)
    end
end

DealDamageEvent.OnServerEvent:Connect(onDealDamageEventTo)

Setup:

1) Create a remote Event inside ReplicatedStorage and Name the event as DealDamage

2) Create a blank Tool and disable the setting "Requires Handle" then create a Local script inside the tool then paste the above local script inside replacing the print("Hello World") text.

3) create a Normal/Sever script inside ServerScriptService and name it anything you like then paste the above server script inside replacing print("Hello World") as you did the local script.

4) add a Humanoid Npc or player to your game (this will be your target)


Checklist and workings:

1)You should have a Tool with the above local script inside your Game.StarterPack

2) you should have a RemoteEvent Named "DealDamage" inside Game.ReplicatedStorage

3) You should have a Server(Normal) Script inside Game.ServerScriptService

4) A target Humanoid

If you've checked all the above, and you have it exactly as I've explained then when you hit play and click on your target Npc/Player they should lose health, and you should have an output of text displaying what the script(s) are doing!

If you have any questions/problems then let me know here and ill get back to you a.s.a.p! :)

Ad

Answer this question