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

Cannot get button to not stop generating parts when clicked once more?

I'm a beginner scripter on Lua and while I was experimenting, I was trying to make it so that a player could click on a specific brick and it would cause parts to be generated until the brick was clicked once more by the player, but I cannot seem to write code that would do that.

Here's what I have so far (I do apologize if the code seems rudimentary, I've only been scripting for a few weeks):

game.Workspace.ButtonPart.Touched:connect(function()
	while true do
		wait()
	local h = Instance.new("Part", game.Workspace)
	h.Position = Vector3.new(36, 0.5, 21.5)
		if game.Workspace.ButtonPart.RightMouseClick then
			break
		end
		
	end
end)

If anyone could help me with this it would be much appreciated!

2 answers

✓ Accepted answer

You will first want to add a ClickDetector into the part. If this is a server script, you would also need to determine the "click state" of the part. This can be done using a BoolValue.

local clickdetector = Instance.new("ClickDetector", game.Workspace.ButtonPart)
local clickstate = Instance.new("BoolValue", clickdetector)
-- clickstate will be true for left click, back to false for right click

clickdetector.MouseClick:Connect(function()
	if clickstate.Value == false then
		clickstate.Value = true -- Set click status to "left clicked"
	end

	while true do wait()
		if clickstate.Value == false then return end -- Check if right clicked

		local h = Instance.new("Part", game.Workspace)
		h.Position = Vector3.new(36, 0.5, 21.5)
    	end
end)

clickdetector.RightMouseClick:Connect(function()
	if clickstate.Value == true then -- Check if left-clicked
		clickstate.Value = false -- Set to right clicked status
	end
end)

2 comments

0

So, to my understanding, the clickstate.Value will determine whether the while loop runs or not, and the while loop only runs as long as clickstate.Value = true, but when the part is clicked with the right side of the mouse, it sets the BoolValue back to false, meaning the while loop will return end.

0

That is correct.

game.Workspace.ButtonPart.Touched:Connect(function()
    while true do
        wait(0.1)
    	local h = Instance.new("Part", game.Workspace)
    	h.Position = Vector3.new(36, 0.5, 21.5)
    	if game.Workspace.ButtonPart.Touched then
            break
        end
    end
end)

i made it so both the times touch

1 comment

0

If he is using touched instead of mouse clicks (I assume clicks meant clicks), you should be doing "game.Workspace.ButtonPart.Touched:Wait() break" instead of what you put for lines 6-8.

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