Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to add a proper, consistant delay to this laser gun script?

I want to include a properly working delay to this laser script. I used script.Disabled, but it seems to have a few inconsistantcies. Any ideas?

mouse.Button1Down:connect(function()
		tool.Ammo.Value = tool.Ammo.Value - 1
		tool.Handle.Fire:Play()
		local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 2048)
		local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = tool.NeonLight.BrickColor
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		local distance = (tool.Handle.CFrame.p - position).magnitude
		beam.Size = Vector3.new(0.1, 0.1, distance)
		beam.CFrame = CFrame.new(tool.Barrel.CFrame.p, position) * CFrame.Angles(math.rad(math.random(-SpreadAngle,SpreadAngle)), math.rad(math.random(-SpreadAngle,SpreadAngle)), 0) * CFrame.new(0, 0, -distance/2)
		game:GetService("Debris"):AddItem(beam, 0.1)

2 comments

0

What do you mean Delay? You mean for it to wait a rate of fire before shooting again?

0

If you're talking about firerate, simply add a wait() at the end. Make the time that it waits (60/xx), with xx being the rate of fire of the weapon.

1 answer

Hi KingPig,

You just need to use a debounce. This is how a debounce works:

local deb = false;
local del = 2; -- This is the delay it will have.

function do_it()
	if not deb then
		deb = true;
		print("Can do it.");
		wait(del);
		deb = false;
	end
end

mouse.Button1Down:Connect(do_it);

##Well, I hope I helped and have a nice day/night.

Thanks,

Best regards,

~~ KingLoneCat

This discussion is preserved as it was. New answers, comments, and votes are closed.