In the second script, I was able to detect if the object the bullet hit was related to a player's humanoid or not. This stopped the part from taking damage and being nil. Now the part is nil again because the bullet could hit the previous ray. How do I fix this?
local tool = script.Parent local player = game:GetService("Players").LocalPlayer local mouse = player:GetMouse() local sound = tool:WaitForChild("Gunfire") local sound2 = tool:WaitForChild("Reload") local reloading = false local contextActionService = game:GetService("ContextActionService") local difference = 0 local replicatedstorage = game:GetService("ReplicatedStorage") local gungui = tool:WaitForChild("GunGUI") local bullets = tool:WaitForChild("Bullets") local reloadtime = 3 -- Remote Events local equipAnimation = replicatedstorage:WaitForChild("EquipAnimation") local headshot = replicatedstorage:WaitForChild("Headshot") local reload2 = replicatedstorage:WaitForChild("Reload") local shootevent = replicatedstorage:WaitForChild("ShootEvent") local unequipanimation = replicatedstorage:WaitForChild("UnequipAnimation") -- Remote Functions local fetchBulletsLeft = replicatedstorage:WaitForChild("FetchBulletsLeft") -- Reloading function function reload() if bullets.Value <= 9 then if reloading == false then reloading = true reload2:FireServer(tool.reload) mouse.Icon = "http://www.roblox.com/asset?id=26573791" player.PlayerGui:WaitForChild("GunGUI").Bar.Bullets.Text = "R" sound2:Play() wait(reloadtime) bullets.Value = 10 player.PlayerGui:WaitForChild("GunGUI").Bar.Bullets.Text = ""..bullets.Value mouse.Icon = "http://www.roblox.com/asset?id=42445293" equipAnimation:FireServer(tool.shoot) reloading = false else --Prevents reload spamming end end end -- When the tool is equipped, the following event will run tool.Equipped:Connect(function(mouse) gungui:Clone().Parent = player.PlayerGui equipAnimation:FireServer(tool.shoot) mouse.Icon = "http://www.roblox.com/asset?id=42445293" mouse.Button1Down:Connect(function() if bullets.Value <=0 or reloading == true then -- Don't Do Anything else local head = game.Workspace[player.Name].Head.CFrame.lookVector local mouse = CFrame.new(game.Workspace[player.Name].Head.Position,mouse.Hit.p).lookVector difference = (head-mouse) local ray = Ray.new(tool.Handle.CFrame.p,(player:GetMouse().Hit.p - tool.Handle.CFrame.p).unit*200) local part,position = game.Workspace:FindPartOnRay(ray,player.Character,false,true) if difference.magnitude < 1.33 then sound:Play() shootevent:FireServer(tool,position,part) bullets.Value = bullets.Value - 1 end end end)
local serverStorage = game:GetService("ServerStorage") local replicatedStorage = game:GetService("ReplicatedStorage") local kills = "Kills" local damage = 5 local damage2 = 10 replicatedStorage.ShootEvent.OnServerEvent:Connect(function(player,tool,position,part) if player.Character.Humanoid.Health <= 0 then -- Nothing Happens else local distance = (tool.Handle.CFrame.p - position).magnitude if game.Workspace:FindFirstChild(player.Name.."'s Trajectory") then game.Workspace:FindFirstChild(player.Name.."'s Trajectory"):Destroy() end local trajectory = Instance.new("Part",game.Workspace) --print(part.Parent.Name.." is the answer") --Optional local smoke = serverStorage.SmokeParticle:Clone() smoke.Parent = tool.Handle trajectory.BrickColor = BrickColor.new("Dark blue") trajectory.Material = "SmoothPlastic" trajectory.Name = player.Name.."'s Trajectory" trajectory.Transparency = 0.5 trajectory.Anchored = true trajectory.Locked = true trajectory.CanCollide = false trajectory.Size = Vector3.new(0.3,0.3,distance) for i = 0, distance,6 do trajectory.CFrame = CFrame.new(tool.Handle.CFrame.p,position) * CFrame.new(0,0,-distance / 2) wait(0.0001) end smoke:Destroy() if part then print(part.Name.." is the part") print(part.Parent.Name.." is the parent" ) print(part.Parent.Parent.Name.." is the ancestor" ) local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid") --trajectory.Name = player.Name.."'s Trajectory" if not humanoid then --part.Parent == game.Workspace or part.Parent.Parent.Name == game.Workspace or part.Parent.Parent.Parent.Name == game.Workspace or part.Name == trajectory.Name then elseif humanoid then if part.Name == "Head" and part.Parent:FindFirstChild("Humanoid").Health > 0 then --Extra stuff: part.ClassName == "Pants" or part.ClassName == "Accessory" or part:FindFirstAncestorOfClass("Accessory") -- Headshot replicatedStorage.Headshot:FireClient(player) humanoid:TakeDamage(damage2) else humanoid:TakeDamage(damage) end end wait() --wait(0.25) if trajectory then trajectory:Destroy() end end end end)
Closed as Non-Descriptive by hiimgoodpack and Ziffixture
This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.
Why was this question closed?