I'm pretty new to Roblox developing. I've been trying to make a raycast gun now, but when I click to fire the gun, the laser parts sometimes fire in the wrong direction. Sometimes, the laser part doesn't show up at all, even though a ray was fired. I think that the issue is with how I'm defining the parts' CFrame in the onShoot function, but I don't know what the problem is.
Local script
--Variables local tool = script.Parent local replicatedStorage = game:GetService("ReplicatedStorage") local remotes = replicatedStorage:WaitForChild("Remotes") local ammoStart = remotes:WaitForChild("AmmoStart") local shoot = remotes:WaitForChild("Shoot") local players = game:GetService("Players") local mouse = players.LocalPlayer:GetMouse() --When tool is equipped tool.Equipped:Connect(function() ammoStart:FireServer() --When player clicks with tool mouse.Button1Down:Connect(function() local bulletStartPosition = tool.Handle.CFrame.p local bulletDestination = mouse.Hit.p shoot:FireServer(bulletStartPosition, bulletDestination) end) end)
Server script
--Variables local replicatedStorage = game:GetService("ReplicatedStorage") local remotes = replicatedStorage:WaitForChild("Remotes") local ammoStart = remotes:WaitForChild("AmmoStart") local shoot = remotes:WaitForChild("Shoot") --Making functions local function ammoMaking(player) --Creates the ammo and clip for the player print(game.Workspace:FindFirstChild(player.Name)) --Creates ammo local playerAmmo = Instance.new("NumberValue") playerAmmo.Name = player.Name.."'s Ammo Count" playerAmmo.Value = 10 playerAmmo.Parent = game:GetService("ServerStorage") print(playerAmmo.Value) --Creates clip local playerClip = Instance.new("NumberValue") playerClip.Name = player.Name.."'s Clip" playerClip.Value = 4 playerClip.Parent = game:GetService("ServerStorage") print(playerClip.Value) end --Executes when player attempts to shoot the gun local function onShoot(player, bulletStartPosition, bulletDestination) local laser = Ray.new(bulletStartPosition, bulletDestination.unit * 500) local part, position = workspace:FindPartOnRay(laser, player.Character, false, true) local distance = (bulletStartPosition - position).magnitude local laserPart = Instance.new("Part") laserPart.BrickColor = BrickColor.new("Bright blue") laserPart.Material = "Neon" laserPart.Transparency = 0.25 laserPart.Anchored = true laserPart.Locked = true laserPart.CanCollide = false laserPart.Size = Vector3.new(0.3, 0.3, distance) laserPart.CFrame = CFrame.new(bulletStartPosition, position) * CFrame.new(0, 0, -distance / 2) laserPart.Parent = game.Workspace end --When a player equips a gun ammoStart.OnServerEvent:Connect(ammoMaking) --When the player clicks with a gun in hand shoot.OnServerEvent:Connect(onShoot)