Still have questions? Join our Discord server and get real time help.
0

# How to make switch that makes object rotate when on?

Simply said my current script looks something like this

local rolling = true

local parent = script.Parent

local function on()
rolling = false
end

local function off()
washing = true
end

if washing == true then
parent.Parent.Roller.CFrame = parent.Parent.Roller.CFrame * CFrame.fromEulerAnglesXYZ(.4,0,0)
wait()
else
parent.Parent.Roller.CFrame = parent.Parent.Roller.CFrame * CFrame.fromEulerAnglesXYZ(0,0,0)
end

local function Click()
if washing == true then off() else on() end

end
parent.ClickDetector.MouseClick:connect(Click)



still it doesn't works... I don't usually script, mostly building but wanted to make some functional buildings so need to face my weakness now. someone help :p

0
There are multiple issues here. We never defined washing, with both the on and off functions you never change your variables back to the opposite state so they'll always be stuck in either true or false, respectively. You need to wrap your if statement in a while loop. You have meaningless spaces and poor indentation, a deprecated Connect. Your Click() function does nothing. DinozCreates 731 — 2mo
0
Try and implement my answer, then when you unavoidably run into issues PLEASE ask for help again in the comments so we can get those fixed. But make sure all pertinent information is given. DinozCreates 731 — 2mo
0
So there is a model which contains two parts: button and roller. Script is located in button alongside with click detector. I changed the script a lot so it got messed up a lot. Forget all the washing parts and replace them with rolling. I actually got cleared up in most of stuff, the biggest question I have now is if there is something equal of "else" used along the while loop. Lukatura 0 — 2mo
0
Example: while rolling == true do (insert rotation part) else (this is what I mean) (insert no rotation stuff) end... this kinda looks dumb, but I hope you get the meaning. Thanks for responding and sorry for late reply ^-^ Lukatura 0 — 2mo

0
Edited 2 months ago

Alright so based on your limited information this is what I've got. I removed washing and just have it using rolling as your bool since I don't know what washing is or what its there for. I fixed the on/off and the click function so it will change back and forth correctly, i'd highly recommend using a debounce but again since I don't know all the details I wont force one into the script. Your if statement wouldn't work so I added a while loop in place, it likely wont work either as it stands but again, without knowing how and what you're doing this is the best I can do.

EDIT: I reread your question and edited out a couple lines of the while loop. If you want it to rotate while "on" and not while "off" then we don't need to tell it "don't rotate", we just don't rotate it.

local rolling = false
local roller = script.Parent.Parent.Roller

local function on()
rolling = true
end

local function off()
rolling = false
end

while rolling == true do
roller.CFrame = roller.CFrame * CFrame.fromEulerAnglesXYZ(.4,0,0)
end

local function Click()
if rolling == false then on() else off() end
end

parent.ClickDetector.MouseClick:Connect(Click)

0
Okay just to give in more details I am building a washing machine at moment. About washing and rolling variables I first used washing, then thought that replacing it with rolling would clarify things up more, but actually made quite a mess. About the script you made I tried it and first of all the old parent variable which was not needed anymore on last line was a mistake but fixed it quick. Lukatura 0 — 2mo
0
Then the script just didn't worked. Tried changing false in line 12 to true, then the roller was rolling but it couldn't stop with button. So as I guess there is something wrong with switch part. As I see else cant be used with while, so I just wonder what to do there. That's all, thanks a lot x3 Lukatura 0 — 2mo
0
Ok so this should work. I simplified the script a little and made the functions make sense. On is on, off is off. For the question about the "else", you can have one in a while loop, the reason its removed is because we want it to move while ON(true), and do nothing as our else. Instead of saying "else do nothing", its more efficient to not say anything. Try what i have now. DinozCreates 731 — 2mo
0
still doesn't works... I don't even get what is the problem anymore... Lukatura 0 — 2mo
0
The script is fine, you're doing something else wrong. DinozCreates 731 — 2mo