You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear! I am trying to make a button where the gravity would change when you would press it. At first, the gravity would be 196.2, then after pressing the button it would change to 0. Then pressing it for a third time would change it back to 196.2. This is going to bbe used for a space game I’m creating.
What is the issue? Include screenshots / videos if possible! The issue is that when I would press the button the first time and second, it works perfectly. After that, if you click it a third time, the gravity goes to 0 but then changes back really quick to 196.2 and does not stay at 0 or 196.2.
What solutions have you tried so far? Did you look for solutions on the Developer Hub? I have looked in the Developer Hub at Changing the Gravity for only one Player topic but that used a part and not ui and does not change the gravity after stepping on the part again. I also looked at video tutorials, but they only focused on having 2 button to change gravity, whereas I only want one.
Note: Sorry that my scripting is really bad, since I just beginning to learn how to script.
This is a local script.
The local script:
--Waiting for player to click button
local moonGravityRatio = 0
local defaultGravity = 0
local moonGravity = defaultGravity
local enabled = false
local debounce = false
script.Parent.MouseButton1Click:Connect(function()
if not debounce then
debounce = true
enabled = not enabled
workspace.Gravity = 0
script.Parent.Text = "Gravity: On"
script.Parent.BackgroundColor3 = Color3.new(0.419608, 1, 0)
script.Parent.BorderColor3 = Color3.new(0.141176, 0.54902, 0)
end
script.Parent.MouseButton1Click:Connect(function()
if workspace.Gravity == 0 then
wait(1)
debounce = false
workspace.Gravity = 196.2
script.Parent.Text = "Gravity: Off"
script.Parent.BackgroundColor3 = Color3.new(0.986252, 0.00712596, 0.0274357)
script.Parent.BorderColor3 = Color3.new(0.501961, 0, 0.00784314)
else workspace.Gravity = 0
end
end
)
end)
I’ve also tried this, but it did not switch the gravity from 196.2 to 0 and back again.
local enabled = false
local debounce = false
script.Parent.MouseButton1Click:Connect(function()
local moonGravityRatio = 0
local defaultGravity = 0
local moonGravity = defaultGravity
if not debounce then
debounce = true
enabled = not enabled
if workspace.Gravity == 196.2 then
workspace.Gravity = 0
end
else workspace.Gravity = 196.2
end
end)```
so I see the problem, you change debounce once, heres what the computer sees:
if debounce is false then do code
i see the debounce is false
debounce = true
changed to true
but then after a few lines there is no debounce = false
so it just ends there, here is an improved version of your code
--Waiting for player to click button
local moonGravityRatio = 0
local defaultGravity = 0
local moonGravity = defaultGravity
local enabled = false
local debounce = false
script.Parent.MouseButton1Click:Connect(function()
if not debounce then
debounce = true
enabled = not enabled
workspace.Gravity = 0
script.Parent.Text = "Gravity: On"
script.Parent.BackgroundColor3 = Color3.new(0.419608, 1, 0)
script.Parent.BorderColor3 = Color3.new(0.141176, 0.54902, 0)
debounce = false
end
script.Parent.MouseButton1Click:Connect(function()
if workspace.Gravity == 0 then
wait(1)
debounce = false
workspace.Gravity = 196.2
script.Parent.Text = "Gravity: Off"
script.Parent.BackgroundColor3 = Color3.new(0.986252, 0.00712596, 0.0274357)
script.Parent.BorderColor3 = Color3.new(0.501961, 0, 0.00784314)
else workspace.Gravity = 0
end
end
)
end)
in addition i see you use mouebutton1click:Connect(function() end) twice.
so what i suggest is to just use if statements. like this:
--Waiting for player to click button
local moonGravityRatio = 0
local defaultGravity = 0
local moonGravity = defaultGravity
local enabled = false
local debounce = false
script.Parent.MouseButton1Click:Connect(function()
if not debounce then
debounce = true
enabled = not enabled
workspace.Gravity = 0
script.Parent.Text = "Gravity: On"
script.Parent.BackgroundColor3 = Color3.new(0.419608, 1, 0)
script.Parent.BorderColor3 = Color3.new(0.141176, 0.54902, 0)
debounce = false
elseif workspace.Gravity == 0 then -- elseif is like if but if the first if statement (like above) doesnt work it will check this
wait(1)
debounce = false
workspace.Gravity = 196.2
script.Parent.Text = "Gravity: Off"
script.Parent.BackgroundColor3 = Color3.new(0.986252, 0.00712596, 0.0274357)
script.Parent.BorderColor3 = Color3.new(0.501961, 0, 0.00784314)
end
end)
Your second code example was almost there, but you had the else statement in the wrong place and didn’t reset the debounce:
local enabled = false
local debounce = false
script.Parent.MouseButton1Click:Connect(function()
local moonGravityRatio = 0
local defaultGravity = 0
local moonGravity = defaultGravity
if not debounce then
debounce = true
enabled = not enabled
if workspace.Gravity == 196.2 then
workspace.Gravity = 0
else
workspace.Gravity = 196.2
end
debounce = false
end
end)
For your code, when I tried it, it brought the gravity to 0 but never back to 196.2. It kept the gravity at 0 because your code checked to see if it was 0, turn it to 0, and keep it that way and never ran the code from workspace.Gravity = 196.2 to end.
And @BadDad2004 I don’t know why your code didn’t do anything to the gravity. It also didn’t error.
local button = script.Parent
local toggle = false
local debounce = false
button.MouseButton1Click:Connect(function()
if debounce then
return
end
debounce = true
toggle = not toggle
workspace.Gravity = if toggle then 0 else 196.2
button.Text = if toggle then "Gravity: On" else "Gravity: Off"
button.BackgroundColor3 = if toggle then Color3.new(0, 1, 0) else Color3.new(1, 0, 0)
button.BorderColor3 = if toggle then Color3.new(0, 0.666, 0) else Color3.new(0.666, 0, 0)
task.wait(1)
debounce = false
end)
Just so you’re aware for future reference you can’t connect multiple callback functions to the same event of an instance, when doing this only the last connection made is valid, all other connections are disconnected and overridden.
Ok, thanks for the info and advice and model. Sorry my code is bad, since I am a beginner scripter and I was getting stumped on this. Let me try this in studio.