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

How to make so gun ignore hats? (raycast gun) (i know how to add ignore list)

Asked by 4 years ago

I made a gun, and when i shooting in head it's not damaging cause of hats. I am new to scripting (not actually new but i'm scripting rarely) so i don't know most of things, like i don't understand return and for "_," loop. i will appreciate any help

Here's my code (server script)

local rem = game.ReplicatedStorage.hit
local temp = workspace.temp
local debris = game:GetService("Debris")
local headDmg = 20
local torsoDmg = 15
local otherDmg = 10
local dist1 = 50
local dist2 = 150
local dist1multi = 1.25
local dist2multi = 1.5

local function shoot(part,distance)
    if part then
        if part.Parent:FindFirstChild('Humanoid') then
            local hum = part.Parent.Humanoid
            if part.Name == "Head" then --Head
                if distance >dist1 and distance <dist2 then
                    hum:TakeDamage(headDmg/dist1multi)
                elseif distance >dist2 then 
                    hum:TakeDamage(headDmg/dist2multi)
                else --normal distance
                    hum:TakeDamage(headDmg)
                end
            elseif part.Name == "Torso" then --Torso
                if distance >dist1 and distance <dist2 then
                    hum:TakeDamage(torsoDmg/dist1multi)
                elseif distance >dist2 then 
                    hum:TakeDamage(torsoDmg/dist2multi)
                else --normal distance
                    hum:TakeDamage(torsoDmg)
                end
            else --Legs, Arms
                if distance >dist1 and distance <dist2 then
                    hum:TakeDamage(otherDmg/dist1multi)
                elseif distance >dist2 then 
                    hum:TakeDamage(otherDmg/dist2multi)
                else --normal distance
                    hum:TakeDamage(otherDmg)
                end
            end
        end
    end
end

local function sfx(tool) --Gun shot sound
    local gssound = game.ServerStorage.sfx.gun_shot
    local sound = gssound:Clone()
    sound.Parent = tool
    sound.Playing = true
    debris:AddItem(sound,2.5)
end

rem.OnServerEvent:Connect(function(plr,mpos,opos,tool)
    local ray = Ray.new(opos,(mpos - opos).Unit * 250)
    local part, hit = workspace:FindPartOnRay(ray,plr.Character)
    local distance = (opos - hit).magnitude
    sfx(tool) --Gun shot sound

    --Damage
    shoot(part,distance)

    --Beam
    local part = Instance.new('Part',temp)
    part.Anchored = true
    part.CanCollide = false
    part.Color = Color3.fromRGB(255,180,0)
    part.Material = 'Neon'
    part.CFrame = CFrame.new(opos, hit) * CFrame.new(0, 0, -distance / 2)
    part.Size = Vector3.new(.1,.1,distance)

    debris:AddItem(part,.05)
end)

1 answer

Log in to vote
0
Answered by 4 years ago

Well, if you want to add damage for hats (which is an accessory), then you have to add another if statement.

-- Example
if part.Parent.Parent:FindFirstChild("Humanoid") and part.Parent:IsA("Accessory") then

The above line will check whether the part it hit was a 'Accessory' or not. This is how your code might look :

local rem = game.ReplicatedStorage.hit
local temp = workspace.temp
local debris = game:GetService("Debris")
local headDmg = 20
local torsoDmg = 15
local otherDmg = 10
local dist1 = 50
local dist2 = 150
local dist1multi = 1.25
local dist2multi = 1.5

local function shoot(part,distance)
    if part then
    if part.Parent.Parent:FindFirstChild("Humanoid") and part.Parent:IsA("Accessory") then
        -- Your Code
    end
        if part.Parent:FindFirstChild('Humanoid') then
            local hum = part.Parent.Humanoid
            if part.Name == "Head" then --Head
                if distance >dist1 and distance <dist2 then
                    hum:TakeDamage(headDmg/dist1multi)
                elseif distance >dist2 then 
                    hum:TakeDamage(headDmg/dist2multi)
                else --normal distance
                    hum:TakeDamage(headDmg)
                end
            elseif part.Name == "Torso" then --Torso
                if distance >dist1 and distance <dist2 then
                    hum:TakeDamage(torsoDmg/dist1multi)
                elseif distance >dist2 then 
                    hum:TakeDamage(torsoDmg/dist2multi)
                else --normal distance
                    hum:TakeDamage(torsoDmg)
                end
            else --Legs, Arms
                if distance >dist1 and distance <dist2 then
                    hum:TakeDamage(otherDmg/dist1multi)
                elseif distance >dist2 then 
                    hum:TakeDamage(otherDmg/dist2multi)
                else --normal distance
                    hum:TakeDamage(otherDmg)
                end
            end
        end
    end
end

local function sfx(tool) --Gun shot sound
    local gssound = game.ServerStorage.sfx.gun_shot
    local sound = gssound:Clone()
    sound.Parent = tool
    sound.Playing = true
    debris:AddItem(sound,2.5)
end

rem.OnServerEvent:Connect(function(plr,mpos,opos,tool)
    local ray = Ray.new(opos,(mpos - opos).Unit * 250)
    local part, hit = workspace:FindPartOnRay(ray,plr.Character)
    local distance = (opos - hit).magnitude
    sfx(tool) --Gun shot sound

    --Damage
    shoot(part,distance)

    --Beam
    local part = Instance.new('Part',temp)
    part.Anchored = true
    part.CanCollide = false
    part.Color = Color3.fromRGB(255,180,0)
    part.Material = 'Neon'
    part.CFrame = CFrame.new(opos, hit) * CFrame.new(0, 0, -distance / 2)
    part.Size = Vector3.new(.1,.1,distance)

    debris:AddItem(part,.05)
end)

Lemme know if it helps!

0
Yeah, but how do i do that it will ignore this hat that have been hitted? i think i need return? ILYUSHkakilLeR2008 0 — 4y
0
Yes, you can do that, also, you have to do something to check whether its a player hat or not. BestCreativeBoy 1395 — 4y
0
ok i will try return but i dont now exactly how to do this ILYUSHkakilLeR2008 0 — 4y
Ad

Answer this question