Problem with gun script

I wanted to make a gun somewhat like a lazer beam. Kind of like a neon ult if you played valorant. But there seems to be a problem with my scripts. I cant figure it out.

it doesn’t go out the fire hold and i can’t think of a way to add some kind of debris i think or something to delete the ray once it hits something.

it does this:

Local script

---{< Services >}---
local UIS: UserInputService = game:GetService("UserInputService")
local TS: TweenService = game:GetService("TweenService")
local StarterGui = game:WaitForChild("StarterGui")
local LocalPlayer = game.Players.LocalPlayer

---{< Events >}---
local Shot = script.Parent.Shoot

---{< Parts >}---
local Gun = script.Parent
local firePart = Gun.Firepart

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local PlayerGui = LocalPlayer.PlayerGui
local GUI = PlayerGui:WaitForChild("Energy")
local Cont = GUI:WaitForChild("Container")
local ChargeBar = Cont.Charge

---{< Modules >}---
local ray = require(Gun.Ray)

---{< Vars >}---
local Charge = 1000
Charge = math.clamp(Charge, 0, 1000)

local debounce = false
local shooting = false

---{< Main Code >}---

Gun.Activated:Connect(function()
	shooting = true
	while shooting and Charge > 0 do
		--print("Shooting")
		ChargeBar:TweenSize(UDim2.new(1, 0, Charge/1000, 0), "Out", "Linear", 0)
		Shot:FireServer(mouse.Hit.Position)
		ray(firePart.Position, mouse.Hit.Position)
		--print(Charge)
		Charge -= 5
		task.wait(0.005)
		if Charge <= 0  then
			print("Energy ran out")
			shooting = false
		end
	end
end)

Gun.Deactivated:Connect(function()
	print("stopped firing")
	shooting = false  -- Reset shooting to false when deactivated
	while Charge < 1000 and not shooting do
		Charge += 2.5
		task.wait(0.005)
		ChargeBar:TweenSize(UDim2.new(1, 0, Charge/1000, 0), "Out", "Linear", 0)
		--print(Charge)
	end
end)

module script (for the vfx)

local defaultSettings = {
	TextureSpeed = 5,
	TextureLength = 5,
	Texture = "rbxassetid://12781800668",

	Color = ColorSequence.new(Color3.fromRGB(251, 255, 0)),
	Width0 = 1,
	Width1 = 1,

	Transparency = NumberSequence.new(0),
	LightEmission = 1,
	FaceCamera = true,
}

return function (Source, Point, Settings)

	local direction = (Point - Source).Unit
	--local distance = (Point - Source).Magnitue

	--properties
	local CBullet = Instance.new("Part")
	CBullet.Anchored = true
	CBullet.CanCollide = false
	CBullet.Size = Vector3.new(0.05, 0.05, 0.05)
	CBullet.Transparency = 1
	CBullet.Parent = workspace

	CBullet.CFrame = CFrame.lookAt(Source, Source + direction)

	-- Attachments
	local AT0 = Instance.new("Attachment")
	AT0.Position = Source
	AT0.Parent = CBullet

	local AT1 = Instance.new("Attachment")
	AT1.Position = Point
	AT1.Parent = CBullet 

	-- Beam Settings
	local beam = Instance.new("Beam")
	for property, value in pairs(defaultSettings) do
		beam[property] = value
	end

	if Settings then
		for property, value in pairs(Settings) do
			beam[property] = value
		end
	end

	beam.Attachment0 = AT0
	beam.Attachment1 = AT1
	beam.Parent = workspace

	print("Firing Beam from", Source, "to", Point)
end

i dont think the server script will be relevant to this problem so i wont post it.
thanks for the help.

2 Likes

What exactly is the problem? Any error messages?

right i forgot about that, i edited the forum for more clarity, mb i was thinking of solutions so i forgot

i figured it out just changed the AT0’s position to Vector3.new(0,0,0) and it came out of the firepart, but for some reason it goes towards the opposite direction of the hit position so im trying to figure that out

1 Like

i’d say i made it how i wanted it to work just need to polish it and improve the script. All the problem is in the module script here is my script so far

local gun = script.Parent

local debris = game:GetService("Debris")
--Events
local hitEvent = gun.Hit

local defaultSettings = {
	TextureSpeed = 5,
	TextureLength = 5,
	Texture = "rbxassetid://12781800668",

	Color = ColorSequence.new(Color3.fromRGB(251, 255, 0)),
	Width0 = 1,
	Width1 = 1,

	Transparency = NumberSequence.new(0),
	LightEmission = 1,
	FaceCamera = true,
}

return function (Source, Point, Settings)

	local direction = (Point - Source).Unit
	local distance = (Point - Source).Magnitude

	--properties
	local CBullet = Instance.new("Part")
	CBullet.Anchored = true
	CBullet.CanCollide = false
	CBullet.Size = Vector3.new(0.05, 0.05, 0.05)
	CBullet.Transparency = 1
	CBullet.Parent = workspace

	CBullet.CFrame = CFrame.lookAt(Source, Source + direction)

	-- Attachments
	local AT0 = Instance.new("Attachment")
	AT0.Position = Vector3.new(0,0,0)
	AT0.Parent = CBullet

	local AT1 = Instance.new("Attachment")
	AT1.Position = Vector3.new(0,0,-distance)
	AT1.Parent = CBullet 

	-- Beam Settings
	local beam = Instance.new("Beam")
	for property, value in pairs(defaultSettings) do
		beam[property] = value
	end

	if Settings then
		for property, value in pairs(Settings) do
			beam[property] = value
		end
	end

	beam.Attachment0 = AT0
	beam.Attachment1 = AT1
	beam.Parent = workspace
	
	--[[hitEvent.OnClientEvent:Connect(function(plr, hit)
		print(hit)
	end)]]--
	task.wait(0.03)
	CBullet:Destroy()
	

	--print("Firing Beam from", Source, "to", Point)
end

final product of the script:

if you guys have any idea on how to make the beam look smoother it’ll very much help. thats what i’d like to improve on right now in my script. also when you move very fast the tracer lacks behind is there any way to fix that?

1 Like

also when you move very fast the tracer lacks behind is there any way to fix that?

How often are you rendering the beam? Are you using run service?

1 Like

oh i wasn’t i was just doing this since i put the function in a while loop

	task.wait(0.03)
	CBullet:Destroy()

i’ll try it out

I tried it out im one step closer to making it as smooth as possible however i encountered another problem if i dont disconnect the heartbeat it just runs continously so the beam is not appearing at all. But if i put a disconnect on it it doesnt destroy the ray. I got no idea how to solve this atm.

heres my currect module script:

local gun = script.Parent

--Services
local RS = game:GetService("RunService")
local debris = game:GetService("Debris")
--Events
local hitEvent = gun.Hit

local defaultSettings = {
	TextureSpeed = 5,
	TextureLength = 5,
	Texture = "rbxassetid://12781800668",

	Color = ColorSequence.new(Color3.fromRGB(251, 255, 0)),
	Width0 = 1,
	Width1 = 1,

	Transparency = NumberSequence.new(0),
	LightEmission = 1,
	FaceCamera = true,
}

return function (Source, Point, Settings)

	local direction = (Point - Source).Unit
	local distance = (Point - Source).Magnitude

	--properties
	local CBullet = Instance.new("Part")
	CBullet.Anchored = true
	CBullet.CanCollide = false
	CBullet.Size = Vector3.new(0.05, 0.05, 0.05)
	CBullet.Transparency = 1
	CBullet.Parent = workspace

	CBullet.CFrame = CFrame.lookAt(Source, Source + direction)

	-- Attachments
	local AT0 = Instance.new("Attachment")
	AT0.Position = Vector3.new(0,0,0)
	AT0.Parent = CBullet

	local AT1 = Instance.new("Attachment")
	AT1.Position = Vector3.new(0,0,-distance)
	AT1.Parent = CBullet 

	-- Beam Settings
	local beam = Instance.new("Beam")
	for property, value in pairs(defaultSettings) do
		beam[property] = value
	end

	if Settings then
		for property, value in pairs(Settings) do
			beam[property] = value
		end
	end

	beam.Attachment0 = AT0
	beam.Attachment1 = AT1
	beam.Parent = workspace
	
	RS.Heartbeat:Connect(function()
		local destroyRay = RS.Heartbeat:Connect(function()
			CBullet:Destroy()
			task.wait()
			print("ray destroyed")
		end)
		destroyRay:Disconnect()
	end)


	--print("Firing Beam from", Source, "to", Point)
end

never mind i fixed it i found a lot of ways i can do it. Where the function works properly but it lags the game like crazy cause the heartbeat never stops i found this method and used it its not as smooth as i want to but its way smoother than my last one

local gun = script.Parent

--Services
local RS = game:GetService("RunService")
local debris = game:GetService("Debris")
--Events
local hitEvent = gun.Hit

local defaultSettings = {
	TextureSpeed = 5,
	TextureLength = 5,
	Texture = "rbxassetid://12781800668",

	Color = ColorSequence.new(Color3.fromRGB(251, 255, 0)),
	Width0 = 1,
	Width1 = 1,

	Transparency = NumberSequence.new(0),
	LightEmission = 1,
	FaceCamera = true,
}

return function (Source, Point, Settings)

	local direction = (Point - Source).Unit
	local distance = (Point - Source).Magnitude

	--properties
	local CBullet = Instance.new("Part")
	CBullet.Anchored = true
	CBullet.CanCollide = false
	CBullet.Size = Vector3.new(0.05, 0.05, 0.05)
	CBullet.Transparency = 1
	CBullet.Parent = workspace

	CBullet.CFrame = CFrame.lookAt(Source, Source + direction)

	-- Attachments
	local AT0 = Instance.new("Attachment")
	AT0.Position = Vector3.new(0,0,0)
	AT0.Parent = CBullet

	local AT1 = Instance.new("Attachment")
	AT1.Position = Vector3.new(0,0,-distance)
	AT1.Parent = CBullet 

	-- Beam Settings
	local beam = Instance.new("Beam")
	for property, value in pairs(defaultSettings) do
		beam[property] = value
	end

	if Settings then
		for property, value in pairs(Settings) do
			beam[property] = value
		end
	end

	beam.Attachment0 = AT0
	beam.Attachment1 = AT1
	beam.Parent = CBullet


	local destroyRay

	-- Function to destroy CBullet and disconnect the heartbeat
	local function destroyAndDisconnect()
		CBullet:Destroy()
		destroyRay:Disconnect()
		print("ray destroyed")
	end

	-- Connect to the heartbeat event with a delay
	destroyRay = RS.Heartbeat:Connect(function()
		task.wait(0.01)  -- Adjust the delay as needed
		destroyAndDisconnect()
	end)

	--print("Firing Beam from", Source, "to", Point)
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.