This is my gun, when I shoot it it fires a laser thing using raycasting, but sometimes when I’m shooting it, the laser for some reason fires somewhere else, here’s what I mean.
I’m shooting towards that stair case over there, but a laser hits the vending machine. Why is this happening? Heres my ray script, could I also improve this script? It is a server script inside of the tool.-- Raycast
script.Parent.Fired.OnServerEvent:Connect(function(player, fromP, toP)
local raycast = Ray.new(fromP, (toP-fromP).unit*100)
local part, position = game.Workspace:FindPartOnRay(raycast, player.Character, false, true)
if part then
local hum = part.Parent:FindFirstChild("Humanoid")
if hum then
hum:TakeDamage(10)
end
end
local laser = Instance.new("Part")
laser.BrickColor = BrickColor.new("Bright yellow")
laser.Parent = game.Workspace
laser.CanCollide = false
laser.Anchored = true
local dist = (toP-fromP).magnitude
laser.CFrame = CFrame.new(fromP, position)*CFrame.new(0,0,-dist/2)
laser.Size = Vector3.new(0.1,0.1, dist)
for i = 1,10 do
wait(.025)
laser.Transparency = laser.Transparency +.1
end
game.Debris:AddItem(laser, data.shoottime)
end)