So I am making a gun using raycast and I'm visualizing the bullet on the clientside to prevent Bullet lag. But I came across a problem where it's not visualizing the bullet. I tried to put Cframe.LookAt but still not working. Can anyone please help me? Local script:

local laser = script.Parent
local handle = script.Parent.Handle
local startbul = handle.Attachment
local replicatedstorage = game:GetService("ReplicatedStorage")
local bullet = replicatedstorage:WaitForChild("laserbullet")
local shooting = false
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.Icon = "http://www.roblox.com/asset/?id=7046646043"
laser.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		shooting = true
		while shooting do
			wait(0.2)
			bullet:FireServer(mouse.hit.Position, startbul)
			laser.lasersound:Play()
		
		mouse.Button1Up:Connect(function()
			shooting = false
		end)
			end
			end)
		end)

Server Script: (Hit Detection)

local replicatedstorage = game:GetService("ReplicatedStorage")
local bullet = replicatedstorage:WaitForChild("laserbullet")
local ServerStorage = game:GetService("ServerStorage")
local laserclient = replicatedstorage:WaitForChild("laserclient")
bullet.OnServerEvent:Connect(function(player, mospos, startbul)
	local origin = startbul.WorldPosition
	local direction = (mospos - origin).Unit
	local result = workspace:Raycast(origin, direction*200)
	if result then
		local hum = result.Instance.Parent:FindFirstChild("Humanoid")
		print(result.Instance.Parent)
		if hum then
			hum:TakeDamage(30)
		end
	end
	local intersection = result and result.Position or origin + direction*200
	local distance = (origin - intersection).Magnitude 
	for _,plr in pairs(game:GetService("Players"):GetPlayers()) do
		print("passed")
			laserclient:FireClient(plr, player.Name, intersection, distance, origin, mospos, direction , result.Position)
			print("passed")
	end
end)

Another Local script: (Visualizing the bullet)

local replicatedstorage = game:GetService("ReplicatedStorage")
local laserclient = replicatedstorage:WaitForChild("laserclient")
local ServerStorage = game:GetService("ServerStorage")
local PlayerService = game:GetService("Players")

laserclient.OnClientEvent:Connect(function(player, playyername, intersection, distance, origin, mospos, direction , pos)
	local Shooter = nil
	for _, Client in pairs(PlayerService:GetPlayers()) do
		print('passed')
	if Client.Name ~= playyername then
			Shooter = Client
			print("passed")
		break
	end
end

if Shooter then
	-- Find that player's character and gun muzzle. Might have to use WaitForChild.
	local Character = Shooter.Character
	local Muzzle = Character:WaitForChild("laser")
    	local bullet_clone = replicatedstorage:WaitForChild("Bullet"):Clone()
bullet_clone.Size = Vector3.new(1, 1, distance)
bullet_clone.CFrame = CFrame.new(origin, pos)*CFrame.new(0, 0, -distance/2)-- This Problem
	bullet_clone.Parent = game.Workspace
		game.Debris:AddItem(bullet_clone, 20)
		print("BULLET SPAWNING")
end
end)

ERROR:

Players.Character.Backpack.laser.LocalScript:23: invalid argument #2 to 'new' (Vector3 expected, got nil) 
  13:10:13.954  Stack Begin  -  Studio
  13:10:13.954  Script 'Players.Character.Backpack.laser.LocalScript', Line 23  -  Studio - LocalScript:23
  13:10:13.954  Stack End  -  Studio

2 comments

0

I think you're supposed to put Vector3.new instead of CFrame.new somewhere in line 23..

0

@ZIRFAL3 I am trying to make the origin face the end position, just like any normal raycast gun