Lazer not forming properly

I have been trying to make a laser gun for our game, and it is quite glitchy(I am a noob :slight_smile: ). Whenever you shoot the gun to a surface very close to the origin of the lazer, the part forming the lazer is instanced just fine. However, if you shoot it at an object greater than about 2.5 studs away, the lazer doesn’t form. Can you see anything that I messed up?

local timer = 0
local p
local hitboi = false
local model
local distance
game.ReplicatedStorage.Remotes.immobilize.OnServerEvent:Connect(function(player,raypostion,origin)
	if timer == 0 then
		timer = 1
		player.Character:FindFirstChild("Immobilizer test").Lazerer.laser_beam:Play()
		if game.Workspace:FindFirstChild("Dart") then
			game.Workspace:FindFirstChild("Dart"):Destroy()
		end
		local raycastParams = RaycastParams.new(player.Character)
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		local lazer = Ray.new(origin, (raypostion - origin)*300)
		if lazer then
			local ignore = {player.Character}
			local hitpart = workspace:FindPartOnRayWithIgnoreList(lazer,ignore)
			if hitpart then
				model = hitpart:FindFirstAncestorOfClass("Model")
			end
		if model then
				if model:FindFirstChild("Humanoid") and model ~= player.Character then
					hitboi = true
				local scriptclone = game.ServerScriptService.ImmobilizerHit:Clone()
				scriptclone.Parent = model:FindFirstChild("Humanoid")
				scriptclone.Disabled = false
			end
		end
	
		distance = ((origin - raypostion)*300).Magnitude
		p = Instance.new("Part",workspace)
		p.Name = "Dart"
		p.Anchored = true
		p.CanCollide = false
		p.Material = "Neon"
		p.BrickColor = BrickColor.new("Bright red")
	end
	
		p.Size = Vector3.new(0.5, 0.5, distance)
		p.CFrame = CFrame.lookAt(origin, raypostion)*CFrame.new(0, 0, -distance/2)
		print(p.Size)
		print(p.Position)
		game.Workspace.Camera.CameraSubject = p
		wait(0.2)
		p:Destroy()
		if hitboi == false then
			timer = 0
		elseif hitboi == true then 
			timer = 0
			hitboi = false
		end
	end
end)

(rayposition is where the player clicked, and origin is the gun tip)

1 Like

I think you used the old raycast part on one of your lines in assigning the variable lazer.

local lazer = Ray.new(origin, (raypostion - origin)*300)

Using Ray.new is depricated / removed now, I think you should be using workspace:Raycast so it works effectively.

Ohhhh that makes sense…brain fart… Thanks!