Gun's Lazerbeam CFrame not following or updating when I get teleported

lazerError.wmv (3.2 MB)

As you can see, my Lazerbeam’s CFrame stops updating to where my laser gun is. I’ve been stuck trying to figure it out for hours now. Here are my scripts:

Client:

local mouse = player:GetMouse()
local lazerGun = script.Parent
local lazerEvent = game:GetService("ReplicatedStorage").LazerEvent

local canShoot = true -- Boolean to track if the player can shoot
local cooldownTime = 3 -- Cooldown time in seconds

lazerGun.Activated:Connect(function()
	if canShoot then
		local targetPos = mouse.Hit.Position
		lazerEvent:FireServer(targetPos)
		canShoot = false -- Disable shooting
		wait(cooldownTime) -- Wait for the cooldown time
		canShoot = true -- Re-enable shooting
	end
end)

Server

local LaserEvent = game:GetService("ReplicatedStorage").LazerEvent
local Debris = game:GetService("Debris")
local Sounds = game:GetService("ReplicatedStorage").Sounds

LaserEvent.OnServerEvent:Connect(function(player, targetPos)
	local character = player.Character
	if not character then return end

	local lazerGun = character:FindFirstChild("LazerGun")
	if not lazerGun then return end

	local topofGun = lazerGun:FindFirstChild("top")
	if not topofGun then return end

	local lazerBeam = Instance.new("Part")
	lazerBeam.Parent = game.Workspace
	lazerBeam.BrickColor = BrickColor.new("Really red")
	lazerBeam.FormFactor = Enum.FormFactor.Custom
	lazerBeam.Material = Enum.Material.Neon
	lazerBeam.Transparency = 0.10
	lazerBeam.Anchored = true
	lazerBeam.CanCollide = false

	lazerBeam.Size = Vector3.new(0.5, 0.5, ((targetPos - topofGun.Position).Magnitude) + 3)
	lazerBeam.CFrame = CFrame.new(topofGun.Position, targetPos) * CFrame.new(0, 0, -lazerBeam.Size.Z / 2)
	
	local function updateLazerBeam()
		if lazerBeam.Parent then
			lazerBeam.CFrame = CFrame.new(topofGun.Position, targetPos) * CFrame.new(0, 0, -lazerBeam.Size.Z / 2)
		end
	end
	
	

	-- Debounce table to track hit players
	local hitPlayers = {}

	lazerBeam.Touched:Connect(function(hit)
		local hitCharacter = hit.Parent
		local humanoid = hitCharacter:FindFirstChildOfClass("Humanoid")

		if humanoid and hitCharacter ~= character then
			local hitPlayer = Players:GetPlayerFromCharacter(hitCharacter)
			if hitPlayer and not hitPlayers[hitPlayer.UserId] then
				hitPlayers[hitPlayer.UserId] = true -- Mark the player as hit
				humanoid.Health = 0 -- Kill the humanoid

				Sounds.DuckSound:Play()
				Sounds.MoneySound:Play()
				print(character.Name.." killed "..hitPlayer.Name)

				local leaderstats = player:FindFirstChild("leaderstats")
				if leaderstats then
					local kills = leaderstats:FindFirstChild("Kills")
					if kills then
						kills.Value = kills.Value + 1
					end
				end
			end
		end
	end)

	Sounds.LazerSound:Play()

	-- Clean up lazerBeam after a duration
	Debris:AddItem(lazerBeam, 1)
	
	--adding this code so you guys can see where the lazer is at all times
	while task.wait() do
			updateLazerBeam()
	end
	
end)
1 Like