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

cool down script won't work?

Ok so I'm making a simple script where on click a part will appear infront of me, move foward, and delete after a distance. (game's like galaga). The issue is I think is it gets caught in the while/do function, disallowing the code to continue.

local cool = 0.75
local function firee()
	local bullet = Instance.new("Part")
	bullet.Parent = workspace
	bullet.Size = Vector3.new(0.75, 0.75, 1.75)
	bullet.Position = Vector3.new(root.Position.X, root.Position.Y, root.Position.Z + 4)
	bullet.Anchored = true
	while bullet do
		bullet.Position = bullet.Position + Vector3.new(0, 0, 1)
		wait(0.001)
		if bullet.Position.Z > 100 then
			bullet:Remove()
		end
	end	
end

mouse.Button1Down:Connect(function()
	if player.Character then
		if debounce then
			debounce = false
			print("Fire")
			firee()
			wait(cool)
			debounce = true
			return
		end
	end
end)

When I remove firee() from the script the print works fine but when I insert firee it breaks. I also found that if I remove the while/do function and everything inside it it works (but of course doesn't move then). Help?>

2 comments

0

that variable error from formatting is really giving my mind death and im not sure why

0

sorry 'bout that, I fixed it.

2 answers

try this

function fire()
	    local bullet = Instance.new("Part")
    bullet.Parent = game.Workspace
	    bullet.Size = Vector3.new(0.75, 0.75, 1.75)
	    bullet.Position = Vector3.new(root.Position.X, root.Position.Y, root.Position.Z + 4)
	    bullet.Anchored = true
	    if bullet then
	        bullet.Position = bullet.Position + UDim2(0, 0, 1)
	        wait(0.001)
	        if bullet.Position.Z > 100 then
	          bullet:Remove()

end
	        end
	    end
	end

--I believe this will fix your problem--

if this does not work i'm truly sorry i don't understand this too much

2 comments

0

That made it worst, the bullet wouldn't move at all.

0

you need to use while bullet do, not if bullet then. Also UDim2 just bring out an error.

I finally figured the answer out myself.

mouse.Button1Down:Connect(function()
	if debounce == true then
		debounce = false
		wait(cool)
		debounce = true
		return
	end
end)

mouse.Button1Down:Connect(function()
	if debounce then
		firee()
	end
end)

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