Hey! I’m working on a simple automatic gun and I was wondering if there is a better way than using a while true do loop to make it work. Is ther a way to do this? Any help would be very appreciated!
current client script:
workspace:WaitForChild(game.Players.LocalPlayer.Name)
local plr = game.Players.LocalPlayer
local char = plr.Character
local tool = script.Parent
while true do
wait(0.1)
if tool.Parent.Parent == char then
-- fire the weapon
end
end
Possibly use coroutines so you can yield the loop when the player stops firing? Although I don’t use coroutines often so I can’t help you with anymore more then a suggestion.
There actually is a better way. It’s called use the condition correctly.
while mouseDown do
fire()
wait()
end
Don’t yield at the start of an iteration, regardless of what loop you’re using, that’s bad practice. Use the condition to determine when the loop should terminate or not. Only if you need a non-terminating loop should you use true as a condition.
I don’t really like repeat in terms of a gun. Feels like my freedom is being taken for handling iterations and it just generally feels improper.