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

How would I fix my gun script?

I want to try making the gun script, automatic. But the problem is I don't know how to trigger it properly. Look down for examples.

Here's an brief-example of what I did so far:

mouse.Button1Down:connect(function()
	mouse.Button1Up:connect(function()
		game.ReplicatedStorage.ROBLOX_MP5FireEvent:FireServer(mouse.Hit.p)
	end)

Then here's my next one, it works fine but it shoots not automatic but pistol type. Where you have to spam click.

mouse.Button1Down:connect(function()
		game.ReplicatedStorage.ROBLOX_MP5FireEvent:FireServer(mouse.Hit.p)
	end)

3 answers

Then what's the real fix to this?

3 comments

1

renderstepped still works fine but if you want use while wait() do

0

Where do I put the while wait() at?

0

and also, it didnt work.

Try to do with some variables. Like,

--This must be a LocalScript
local FireRate = 0.1
local on = false
local PL = game.Players.LocalPlayer
local MS = PL:GetMouse()
local MSPos = MS.Hit.p
local SEvent = game.ReaplicatedStorage.ROBLOX_MP5FireEvent
function Enable()
	on = true
end

function Disable()
	on = false
end

while on == true do
	wait(FireRate)
	SEvent:FireServer(MSPos)
end

PM me on ROBLOX and say me if it works (Loulou1112)

1 comment

0

Variables don't help more - in fact, in most cases it's better to refer directly to your goal than use variable over variable which variables you are probably only going to use 2 or 3 of them which other variables you used to create the variables you'll actually use. In other words, this doesn't solve anything.

What you can do is make a variable and use renderstepped.

local fire = false

game:GetService.RunSerivce:RenderStepped:connect(function()
	if fire == true then
	  game.ReplicatedStorage.ROBLOX_MP5FireEvent:FireServer(mouse.Hit.p)
	end
end)

mouse.Button1Down:connect(function()
       fire = true
    end)
mouse.Button1Up:connect(function()
        fire = false
    end)

if this helped please accept :) ~KIHeros

Oh and if you want a firerate you can add a firerate variable

local fireRate = 0.2

--then make it wait the fireRate

if fire == true then
  wait(fireRate)
  game.ReplicatedStorage.ROBLOX_MP5FireEvent:FireServer(mouse.Hit.p)
end

1 comment

1

game:GetService.RunSerivce.RenderStepped:connect()* Also, RenderStepped fires every time the client renders, and if the server keeps recieving events via the network on every client's render it can get laggy.

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