Why does my debounce not work, yet produce no errors? - lua

Why does my debounce not work, yet produce no errors?
Here's my script so far, but it isn't working.
local UIS = game:GetService("UserInputService")
local toggle = false
script.Parent.BackpackFrame.Visible = false
script.Parent.BackpackText.Visible = false
UIS.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
if toggle == false then
script.Parent.BackpackFrame.Visible = true
script.Parent.BackpackText.Visible = true
toggle = true
end
if toggle == true then
script.Parent.BackpackFrame.Visible = false
script.Parent.BackpackText.Visible = false
toggle = false
end
end
end)

If toggle is false you set it to true
if toggle == false then
script.Parent.BackpackFrame.Visible = true
script.Parent.BackpackText.Visible = true
toggle = true
end
And then immediately set it to false as you run into your second conditional statement.
if toggle == true then
script.Parent.BackpackFrame.Visible = false
script.Parent.BackpackText.Visible = false
toggle = false
end
So your change is revoked immediately befor anything had a chance to update.
If you have two exclusive states use if / else.
if toggle == false then
script.Parent.BackpackFrame.Visible = true
script.Parent.BackpackText.Visible = true
toggle = true
else
script.Parent.BackpackFrame.Visible = false
script.Parent.BackpackText.Visible = false
toggle = false
end
As all you're doing is to assign boolean values you can simply get rid of your conditions and do this:
toggle = not toggle -- toggle your state
-- assign the state to the visible property
script.Parent.BackpackFrame.Visible = toggle
script.Parent.BackpackText.Visibile = toggle

The code is setting toggle to false immediately after setting it to true.
if toggle == false then
-- false part
toggle = true
end
if toggle == true then
-- true part
toggle = false
end
Just use an else instead of checking toggle twice
if toggle then
-- true part
toggle = false
else
-- false part
toggle = true
end

Related

attempt to perform arithmetic (add) on string and number Error

when I touch the object, I want it to be deleted and come back after 2 seconds and add 1 coin. how can I do this?
local WheatAmount = script.Parent
local oyuncuParasi = "Coins"
duvar = false
function onTouch(vurus)
if duvar == false then
oyuncuParasi = oyuncuParasi +1;
duvar = true
vurus.Parent:Destroy()
WheatAmount.CanCollide = false
wait(2)
WheatAmount.CanCollide = true
duvar = false
end
end
script.Parent.Touched:connect(onTouch)
You are trying to add 1 to local oyuncuParasi = "Coins" which is currently a string...
Try this:
local WheatAmount = script.Parent
local oyuncuParasi = 0
duvar = false
function onTouch(vurus)
if duvar == false then
oyuncuParasi += 1; -- Adds a coin
duvar = true
WheatAmount.Transparent = 1 -- Set the object Transparent
WheatAmount.CanCollide = false
WheatAmount.CanTouch = false
wait(2)
WheatAmount.Transparent = 0 -- Set the object Not Transparent
WheatAmount.CanCollide = true
WheatAmount.CanTouch = true
duvar = false
end
end
script.Parent.Touched:connect(onTouch)
Note: Make sure you put this in a server sided script instead of local script in the Object.
Let me know if it worked and the results!!

Is there a way to Disconnect the Function from UIS.InputBegan:Connect(...)?

I tried everyway to somehwo make this work... It works on the first time you execute it, but if you try to execute it again it just fails. Here is the code:
script.Parent.MouseButton1Down:Connect(function()
KS.Visible = true
KSIO = true
wait(.5)
UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
if KSIO == true then
script.Parent.Text = Input.KeyCode.Name
KS.Visible = false
KSIO = false
end
print(KSIO)
end)
end)
If you want to disconnect the event, then you can do something like this to disconnect the event.
local conn
function handle(parm1, parm2)
-- logic goes here
if somethingistrue then
conn:Disconnect()
end
end
conn = UIS.InputBegan:Connect(handle)
You want to look at globals. Globals in lua are variables that can be accessed from another thread.
You might be looking for this.
if getgenv().HasRanBefore then return end
if not getgenv().HasRanBefore then getgenv().HasRanBefore = true end
script.Parent.MouseButton1Down:Connect(function()
KS.Visible = true
KSIO = true
wait(.5)
UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
if KSIO == true then
script.Parent.Text = Input.KeyCode.Name
KS.Visible = false
KSIO = false
end
print(KSIO)
end)
end)

Proximity prompt issue

basically after my math.random it will teleport you to a location and then after 20 seconds it will teleport you back. The proximity prompt that this script is in only fires this script once, i click the button to go to sleep and it does nothing, no error code no thing. On the other hand after the else statement i can click the button three separate times and then if i try clicking it a fourth time it no longer works, still with no error.
Is there anyway I can fix this?
Heres my code if helpful
game.Players.PlayerAdded:Connect(function(player)
script.Parent.Triggered:Connect(function(activated)
if activated then
print("activated")
if game.ReplicatedFirst.night.Value == true then
script.Parent.Enabled = false
local number = math.random(1, 2)
print(number)
if number == 1 then
local plr = game.Workspace:FindFirstChild(player.Name)
game.ReplicatedFirst.slep.Value = true
plr.Humanoid.HipHeight = -0.913
plr.HumanoidRootPart.Position = Vector3.new(327.227, 1.5, -348.899) + Vector3.new(0,10,0)
print("ho")
player.PlayerGui.slepgui.Enabled = false
wait(20)
game.Workspace:FindFirstChild(player.Name).HumanoidRootPart.Position = Vector3.new(31.546, 20.793, 12.62) + Vector3.new(0,10,0)
script.Parent.Enabled = true
game.ReplicatedFirst.night.Value = false
game.ReplicatedFirst.slep.Value = false
game.Workspace["Hotel - undertale"].Playing = true
game.Lighting.TimeOfDay = 12
else
local plr = game.Workspace:FindFirstChild(player.Name)
player.PlayerGui.slepgui.Enabled = true
script.Parent.Enabled = false
game.ReplicatedFirst.slep.Value = true
script.Parent.ObjectText = "Go to bed, this will skip the night"
game.Workspace["SNORING - SOUND EFFECT (128 kbps)"].Playing = true
print("number one worked")
wait(12)
plr.HumanoidRootPart.Position = Vector3.new(31.546, 20.793, 12.62) + Vector3.new(0,10,0)
game.ReplicatedFirst.night.Value = false
game.ReplicatedFirst.slep.Value = false
game.Workspace["Hotel - undertale"].Playing = true
player.PlayerGui.slepgui.Enabled = false
script.Parent.Enabled = true
game.Lighting.TimeOfDay = 12
script.Parent.ObjectText = "Go to bed. Only avaliable at night"
print("Numver two worked")
end
end
end
end)
end)
-- 327.227, 1.5, -348.899

local a = true print(a and false or true) why result always be true whether a==false or a == true?

Why is a not correct?
I can't understand.
Code:
C:\Users\Administrator>lua53
Lua 5.3.4 Copyright (C) 1994-2017 Lua.org, PUC-Rio
> local a = true print(a and false or true)
true
> local a = false print(a and false or true)
true
> local a = false print(a == true and false or true)
true
> local a = true print(a == true and false or true)
true
> local a = true print(a == false and false or true)
true
> local a = false print(a == false and false or true)
true
> local a = false print((a == false) and false or true)
true
> local a = false print(not a )
true
> local a = true print(not a )
false
>
In Lua, and and or have the lowest operator precedence, with or being lower than and. Therefore, any expression of the form X and false or true will be interpreted as (X and false) or true.
Well, anything logically and-ed with false is false; that's how logical and works. And anything logically or-ed with true is true. And since or comes last, all of those expressions are just over-complicated ways of saying true.

Using a Lua Script to send inputs to BizHawk

I've tried using joypad.set(Inputs), but that doesn't seem to do anything. Also joypad.write(Inputs) doesn't even work. I'm stuck in a corner, and I don't know what to do.
This example works for when emulating a GBA. It will make the emulator press right every frame
input = {}
input['A'] = false
input['B'] = false
input['Down'] = false
input['L'] = false
input['Left'] = false
input['Power'] = false
input['R'] = false
input['Right'] = true
input['Select'] = false
input['Start'] = false
input['Up'] = false
while true do
joypad.set(input)
emu.frameadvance()
end
depending what you emulating this also might be useful
input['P1 A'] = false

Resources