Wanting to create a lever that activates/deactivates a script!

Hello all! I’ve been building a medieval city for quite some time in hopes of making a truly open-world story driven game with turn based combat similar to Pokemon where you’ll have to fight mystical/fantasy creatures… (What a mouthful)

I’ve been dedicated to give players access to every inch of the world. So basically, EVERY building is accessible and the world is something to have fun exploring. However, I want to make it as close to realistic as I can while still keeping that fantasy vibe intact.

The issue I feel like is easily resolvable, I’m not sure if I should put this in scripting support or building support. However, I’ve constructed this sawmill. And for now all I would like to achieve is a lever that would activate and deactivate the script that makes these blocks move up and down (The saw mechanism) because I don’t really want it to be constantly in action.

I’m usually decent at editing already made scripts, such as the ones you see in place here, but I can’t seem to find any resources online to help me with this small issue.

Would appreciate the help! Thank you!

Video of the mill:

URL of the map:

2 Likes

Hello!
You can achieve this by many ways:

  1. Insert a ClickDetector and a Script in the lever part (the script could be parented basically anywhere else inside the workspace). If you insert a code like this one, your mill will be activated/deactivated when someone clicks it (it also works when mobile/tablet users tap the part).
local Lever = workspace.Lever --examples
local MillScript = workspace.Mill.Script
Lever.ClickDetector.MouseClick:Connect(function()
    if MillScript.Disabled == true then
        MillScript.Disabled = false
    else
        MillScript.Disabled = true
    end
    -- OR: insert the mill script code here
end)

You can even change the mouse icon when hovering over the lever part!

  1. The lever could also be activated when a player touches it:
script.Parent.Touched:Connect(function()
    --code
end)

Don’t forget to add debounce!

  1. Use GetMouse() in a LocalScript to bind the script to a user input. This is harder to implement, but you won’t likely need to use this. Since that function can only be called in the client, you would need to use a correct way to send data to the server.

There are probably other ways to do this (maybe using a SurfaceGui?), but these are the most common and intuitive (and easy to implement). :smiley:

PD: Btw, your map looks great! Keep with the good work. :wink:

3 Likes

Thank you! I will have to work on it and dissect this reply! :grinning:

1 Like