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)
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!