I’ve been trying to make a simple hit-scan gun using raycasting and I’ve stumbled into a big error.
Error:
Whenever the mouse is hovering on the character I wish to shoot at, it does no damage, despite the bullet being directly at the mouse’s location. However, when I move it at an angle, it works, but to a degree.
Another error the gun seems to have is when the bullet has to look for the humanoid of the character. It would eventually break the gun and return the error in the output which I put in the link below.
Screenshot of the error: https://gyazo.com/d2981eba53beb52ada3da72b8b602195
I’ve tried going on youtube to look at different methods and each attempt yields the same error, a nil value.
–Server
local damage = 13
game.ReplicatedStorage.Damage.OnServerEvent:Connect(function(Player, MuzPos, BigPos,tool,target)
local raycast = Ray.new(BigPos, (MuzPos - BigPos).unit * 1500)
local Part,Position = game.Workspace:FindPartOnRay(raycast, Player.Character, true, false)
--Skipped out on the less important stuff in the script. The bullet was already defined before as an Instance.new
local dist = (MuzPos - BigPos).magnitude
bullet.CFrame = CFrame.new(BigPos,Position) * CFrame.new(0,0,-dist/2)
bullet.Size = Vector3.new(0.1,0.1,dist)
if Part then
local hum1 = Part.Parent:FindFirstChild("Humanoid")
local hum2 = Part.Parent.Parent:FindFirstChild("Humanoid")
local hum3 = Part.Parent.Parent.Parent:FindFirstChild("Humanoid")
if hum1 then
hum1:TakeDamage(damage)
elseif hum2 then
hum2:TakeDamage(damage)
elseif hum3 then
hum3:TakeDamage(damage)
end
end
game:GetService("Debris"):AddItem(bullet,0.05)
end)
-LocalScript
game.ReplicatedStorage.Damage:FireServer(tool.SmokePart.Position,mouse.Hit.Position,tool,mouse.Target)
--Everything else is just basic stuff about the gun like ammo and other jazz.
Like I previously stated, it is a simple hit-scan gun.