Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
0
Answered by
Sparks 534 Moderation Voter
4 years ago
Edited 4 years ago

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)
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. KingKobraTheBoss 4 — 4y
0
That is correct. Sparks 534 — 4y
Ad
Log in to vote
0
Answered by
panvvan 20
4 years ago
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

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. Sparks 534 — 4y

Answer this question