I am having some issues with my gun, I use a few systems to take the damage to make sure if they hit the character’s accessory then it still damages them. I have looked at other posts, but none of them work in this context. This is the code source:
script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)
if script.Parent.Ammo.Value > 0 then
script.Parent.ReloadServer.Disabled = true
script.Parent.Ammo.Value = script.Parent.Ammo.Value - 1
local Head = player.Character.Head.Position
local FromP = script.Parent.Tip.Position
local ToP = mousePos
local RayCast = Ray.new(Head, (ToP - Head).unit*1000)
local Part, Position = game.Workspace:FindPartOnRay(RayCast, player.Character, false, true)
if Part then
if Part.Parent:FindFirstChild("Humanoid") then
Part.Parent.Humanoid:TakeDamage(26)
elseif Part.Parent.Parent:FindFirstChild("Humanoid") then
Part.Parent.Parent.Humanoid:TakeDamage(26)
elseif Part.Parent.Parent.Parent:FindFirstChild("Humanoid") then
Part.Parent.Parent.Parent.Humanoid:TakeDamage(26)
elseif Part.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid") then
Part.Parent.Parent.Parent.Parent.Humanoid:TakeDamage(26)
end
end
local visualBullet = Instance.new("Part")
visualBullet.Parent = game.Workspace
visualBullet.CanCollide = false
visualBullet.Anchored = true
local Dist = (ToP - FromP).magnitude
visualBullet.CFrame = CFrame.new(FromP,Position)*CFrame.new(0,0,-Dist/2)
visualBullet.Size = Vector3.new(0.3,0.3,Dist)
visualBullet.Material = Enum.Material.Neon
visualBullet.Color = Color3.fromRGB(255, 239, 114)
visualBullet.Name = "GunBullet"
script.Parent.Tip.Flash.Enabled = true
script.Parent.Handle.Fired:Play()
wait(0.04)
visualBullet:Destroy()
script.Parent.Tip.Flash.Enabled = false
script.Parent.ReloadServer.Disabled = false
end
Well from the error it looks like in one of the “if” or “else if” statements, the Part.Parent, or the Part.Parent.Parent etc. is returning nil. Where is this part in the explorer? If it was a direct child of the workspace then part.Parent.Parent.Parent wouldn’t work because it would go part->Workspace->game-> nil. Do you want to add some prints write below “if Part then”.
Add the following and tell us what each of them prints please.
script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)
if script.Parent.Ammo.Value > 0 then
script.Parent.ReloadServer.Disabled = true
script.Parent.Ammo.Value = script.Parent.Ammo.Value - 1
local Head = player.Character.Head.Position
local FromP = script.Parent.Tip.Position
local ToP = mousePos
local RayCast = Ray.new(Head, (ToP - Head).unit*1000)
local Part, Position = game.Workspace:FindPartOnRay(RayCast, player.Character, false, true)
if Part then
if Part.Parent:FindFirstChild("Humanoid") then
Part.Parent.Humanoid:TakeDamage(26)
end
if Part.Parent.Parent:FindFirstChild("Humanoid") then
Part.Parent.Parent.Humanoid:TakeDamage(26)
end
if Part.Parent.Parent.Parent:FindFirstChild("Humanoid") then
Part.Parent.Parent.Parent.Humanoid:TakeDamage(26)
end
if Part.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid") then
Part.Parent.Parent.Parent.Parent.Humanoid:TakeDamage(26)
end
end
local visualBullet = Instance.new("Part")
visualBullet.Parent = game.Workspace
visualBullet.CanCollide = false
visualBullet.Anchored = true
local Dist = (ToP - FromP).magnitude
visualBullet.CFrame = CFrame.new(FromP,Position)*CFrame.new(0,0,-Dist/2)
visualBullet.Size = Vector3.new(0.3,0.3,Dist)
visualBullet.Material = Enum.Material.Neon
visualBullet.Color = Color3.fromRGB(255, 239, 114)
visualBullet.Name = "GunBullet"
script.Parent.Tip.Flash.Enabled = true
script.Parent.Handle.Fired:Play()
wait(0.04)
visualBullet:Destroy()
script.Parent.Tip.Flash.Enabled = false
script.Parent.ReloadServer.Disabled = false
end
end)
Could you add the prints so we can help debug it. Please add the prints and tell me what they say print out so that I can hep you. Do you know what printing is? Also where is this gun, is it a model in the workspace? Is it a single part in the workspace? Is it in a folder?
You fire at the base plate. The script checks if humanoid is in the base plates parent, workspace. It’s not. The script checks if humanoid is in workspaces parent, game. It’s not. The script tries to check if humanoid is in games parent. Game has no parent, the script breaks.
You need to check that Part. Parent. Parent (and so on) actually exists