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

How to make a loop end whenever a player deactivates a tool? (basically, an automatic weapon)

NOTE: I am aware that this probably does not follow the community guideline of own work; and I apologize for that. If this is going to be moderated, please comment to notify me so I can instead delete the post. I am asking here because I cannot find a guide for this anywhere; not on YouTube, wiki.roblox.com, nor Google in general—there is no tutorial I can find on the web regarding my question.

So I'm just messing around and turning ROBLOX sets weapons into what are basically makeshift machine guns/full auto weapons.

I want a weapon (more specifically, a slingshot) to fire repeatedly until (wink) the player releases LMB; when the tool deactivates.

However. that would mean listening to an event (the Deactivated event) to stop the repeat, and from my past experience, nada (that don't work)

So, how do I achieve this?

This is basically what I'm trying to achieve:

-- everything else in a slingshot above
Tool.Enabled = true
function onActivated()

	if not Tool.Enabled then
		return
	end

	Tool.Enabled = false

	local character = Tool.Parent;
	local humanoid = character.Humanoid
	if humanoid == nil then
		print("Humanoid not found")
		return 
	end

	local targetPos = humanoid.TargetPoint
	-- here it is
	repeat
		fire(targetPos)
	until Tool.Deactivated -- the part that probably won't work
	wait(1)

	Tool.Enabled = true
end

script.Parent.Activated:connect(onActivated)

if u didn't read the whole "NOTE:" thing at the top already (THE WHOLE THING) then read it now

2 comments

0

That isn't what the Deactivated event does.

0

??? What do you mean?

1 answer

✓ Accepted answer

This should do the trick. Variables for the win.

Tool.Enabled = true
local Activated = false
function onActivated()
if not Tool.Enabled then return end
Tool.Enabled = false
local Character = Tool.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
if Humanoid == nil then print("Humanoid not found.") return end
Activated = true
local targetPos = Humanoid.TargetPoint
repeat
fire(targetPos)
until not Activated
wait(1)
Tool.Enabled = true
end
script.Parent.Activated:Connect(onActivated)
Tool.Deactivated:Connect(function()
Activated = false
end)

1 comment

0

Thank you, but could you use indentation next time?

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