Why is this code not starting? - lua

I am trying to simulate a Simon Says game. I am currently using Corona SDK, and using Lua as my language.
I have put the addEventListener(touch) on the function, "starting". I have tried debugging it and it seems that the code does not correctly run the part that says, if (started) then statement. I would be very grateful if you guys were to help lead me to the right direction!
Here's part of my code:
local function starting(event)
if (started == false) then
started = true
end
if (event.phase == "ended") then
start()
startText.isVisible = false
---------------------------------------------------
--THIS BELOW PART IS WRONG-- does not do "started"
if (started) then
count = count + 1
--if started--
if(math.mod(count,20) == 0) then
clicked = 0
if(light >= 0) then
light = light - 1
end
end

Make sure you have initialized started as false like rpattiso mentioned. Also in my past experience with Lua, sometimes just putting if (started) then
does not work for me, so you should try if (started == true) then.

Related

Making a frame visible is possible but making invisible is not

script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Frame.Visible == true then
script.Parent.Parent.Frame.Visible = false
end
if script.Parent.Parent.Frame.Visible == false then
script.Parent.Parent.Frame.Visible = true
end
end)
I am very new to coding (have started practically yesterday ) and ive decided to start with lua in roblox studio. The program here is working i can assure you (some things in code may not be in proper place since im having a bit trouble writing code on this site) but the code works. I have tried to make it seperatly for 2 buttons then I tried this and nothing seems to work. I am starting to think it is caused by the program itself and not the code since I can make other things invisible with that code. I've placed those 2 codes for visibility and invisibility in 1 thing since I've been told that events caused by the code happen at same time so some of them may not work.
Assume Visible is true. The first condition will be true and Visible will be set to false. Then, in the next condition Visible is false and the condition is therefore true, again. And it sets Visible back to true.
Now, in order to fix it you want to execute the second condition only if the first failed. Take a look at elseif https://www.lua.org/pil/4.3.1.html.
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Frame.Visible == true then
script.Parent.Parent.Frame.Visible = false
elseif script.Parent.Parent.Frame.Visible == false then
script.Parent.Parent.Frame.Visible = true
end
end)
This code is still quite bulky and can be improved:
script.Parent.MouseButton1Click:Connect(function()
script.Parent.Parent.Frame.Visible = not script.Parent.Parent.Frame.Visible
end)
Now you assign true to Visible if Visible does not evaluate to true. It basically toggles the boolean.

Is there a way to add time to a Wait class inside an If statement?

I started learning LUA a few days ago, started my own project inside Tabletop Simulator game, but I've hit a brick wall. I can't add time to a Wait class.
This is an example of what I tried:
function state_check()
--This function checks if the state of obj_1 and obj_2 is 0 or 1
end
function press_button()
--This function activates other functions based on the state of obj_1 and obj_2
i = 1 --Function starts with a wait of 1 second
if obj_1_state == 1 then --If state is 1, then the function is triggered and 1 second is added to i
Wait.time(func_1, i)
i = i + 1
end
if obj_2_state == 1 then
Wait.time(func_2, i)
end
end
I need for the function to check the first part and if true, do the second part 1 second later. If not, do the second part normally and skip the "i = i + 1".
My problem is that the function does everything at the same time. I know I'm doing something wrong, but I can't figure out what. Is there a way to create some for of gate to do everything in order or anything similar?
Your code seems correct.
I don't know what is the problem.
But I know that one of possible solutions is to follow the "callback hell" style of programming:
local function second_part(obj_2_state)
if obj_2_state == 1 then
Wait.time(func_2, 1)
end
end
local function first_part(obj_1_state, obj_2_state)
if obj_1_state == 1 then
Wait.time(
function()
func_1()
second_part(obj_2_state)
end, 1)
else
second_part(obj_2_state)
end
end
function press_button()
local obj_1_state, obj_2_state = --calculate states of obj_1 and obj_2 here
first_part(obj_1_state, obj_2_state)
end

ROBLOX - If statement won't run inside of while loop?

I've run into something really strange. I was originally trying to make a music script (more like system, tbh) and it had the same problem. Now I have boiled it down to a simple test:
while true do
if script.Parent.musicstate.Value == true then
print("Play")
elseif script.Parent.musicstate.Value == false then
print("Stop")
end
wait()
end
When I run it, it will either only print "Stop" or only print "Play." The string it will print will depend on the value of the bool value when the game starts, as in it does not change what it prints depending on the current value. All help will be appreciated, thanks!
The structure, SongScript is what is running:
Never ever make such an infinite loop crashing your whole system! There is a lot more efficient way to do. You have the problem due to the fact that the script would auto break so it won't crash your whole system.
script.Parent.musicstate.Changed:Connect(function()
if script.Parent.musicstate.Value == true then
print("Play")
elseif script.Parent.musicstate.Value == false then
print("Stop")
end
end)

Need help on ROBLOX script (Lua)

I`m making a script for my UFO in ROBLOX where whenever the UFO passes overhead it plays an audio. I made a script that goes as follows
while true do
if script.Parent.Parent.Velocity.Magnitude>10 then
if local h = hit.Parent:FindFirstChild("Humanoid")
then script.parent:play()
wait(5)
else
wait()
end
wait()
end
Any corrections would be a real help!
Thanks!
Everything aside, at the root there are a lot of syntax errors as well as the error of implementation, all in all the code block you've given us won't do what you wanted it to.
However, here's a basic idea of what the solution is:
local newThread = coroutine.create(function()
while true do
for i, v in game.Players:GetPlayers()
local playerListener = workspace[v.Name]["Head"]
local ufoListener = workspace.Ufo:FindFirstChild('Listener')
local magnitude = (playerListener.Position - ufoListener.Position).magnitude
if (magnitude > 10) then
script.Parent:play()
else
wait(0.5)
end
end
end
end)
coroutine.resume(newThread)
Essentially, here's the rundown: We're using coroutines so this while loop doesn't yield this thread for all eternity. There are some more complex avenues using bindable events and maybe some server-client handshake where, when fired from the LocalPlayer, it calls an event in the server- but I digress. This should be perfect for your needs without confusing you too much.

Implementing TFlers in my game

i'm trying to design a game that was initially designed for 1680 x 1050.. however i implemented Tflers in my project and it not going that well.
First in my love.load i added:
function love.load()
TLfres.setScreen({w= 1680, h = 1200, full=true, vsync = false, aa=0}, 1, false, true)
end
Then in my love.draw():
function love.draw()
TLfres.transform()
love.graphics.setBackgroundColor(0,0,0)
if GameState == "MainMenu" then
love.graphics.draw(background,0,0)
love.graphics.draw(picture, 200, 200)
end
if GameState == "PaintGame" then
love.graphics.setBackgroundColor(20,191,243)
love.graphics.draw(easel, 640,360)
love.graphics.draw(cursor, love.mouse.getX(), love.mouse.getY())
end
TLfres.letterbox(16,9)
end
However i'm getting this error:
TLfres.lua.13: attempt to call field 'setMode' ( a nil value )
How to get rid of this error??
I realized that tlfres requires the old verison of love in order to work, however I want it on the newest version of love. how can I optimize it to work? Thanks!
Thanks guys!
I would add a comment, but I don't have enough reputation.
As of LÖVE 0.9.0, love.graphics.setMode was replaced with love.window.setMode. Talk with the library creator about that.

Resources