My timer script for minecraft is not working - lua

I have a problem with my Minecraft timer script I tried to add a function that saves the time values from worlds in a document. The timer worked completely fine before i added this function...
Code:
name = "Timer"
description = "Just a normal Timer."
positionX = 0
positionY = 0
sizeX = 24
sizeY = 10
scale = 1
START_STOP_KEY = 0x55 --or 'U'
RESET_KEY = 0x4A --or 'J'
--
--[[
if you wish to change the key you can take the key code from here
https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
]] -------------script-code-------------
previoustime = 0
state = 0
startTime = 0
stopTime = 0
ImportedLib = importLib("readfile.lua")
function keyboard(key, isDown)
if (isDown == true) then
if (key == RESET_KEY) then
state = 0
elseif (key == START_STOP_KEY) then
if (state == 0) then
state = 1
startTime = os.time()
elseif (state == 1) then
state = 2
previoustime = os.time()
stopTime = os.time()
elseif (state == 2) then
state = 1
startTime = startTime + os.time() - stopTime
end
end
end
end
TimerText = "00:00"
TextColor = {r = 30, g = 255, b = 30, a = 255}
function doubleDigit(number)
if (number < 10) then
return "0" .. math.floor(number)
else
return math.floor(number)
end
end
function timeText(time)
local result = ""
local days = 0
while (time > 86399) do
days = days + 1
time = time - 86400
end
local hours = 0
while (time > 3599) do
hours = hours + 1
time = time - 86400
end
local minutes = 0
while (time > 59) do
minutes = minutes + 1
time = time - 60
end
if (days == 0) then
if (hours == 0) then
return doubleDigit(minutes) .. ":" .. doubleDigit(time)
else
return math.floor(hours) .. " : " .. doubleDigit(minutes) .. ":" .. doubleDigit(time)
end
else
return math.floor(days) ..
" : " .. doubleDigit(hours) .. " : " .. doubleDigit(minutes) .. ":" .. doubleDigit(time)
end
end
function update()
if (state == 0) then
TextColor = {r = 255, g = 0, b = 0, a = 255}
TimerText = "00:00"
elseif (state == 1) then
TimerText = timeText(os.time() - startTime)
TextColor = {r = 0, g = 255, b = 255, a = 255}
elseif (state == 2) then
TimerText = timeText(stopTime - startTime)
TextColor = {r = 255, g = 255, b = 0, a = 255}
end
end
function render()
local font = gui.font()
local tw = font.width(TimerText)
gfx.color(0, 0, 0, 0)
gfx.rect(0, 0, tw + 4, 10)
gfx.color(TextColor.r, TextColor.g, TextColor.b, TextColor.a)
gfx.text(2, 1, TimerText)
end
This is the function i want to add
if (state == 1) then
local worldName = server.worldName()
io.open(local worldName".txt", "w")
io.output(file)
io.write(time)
io.close(file)
end
if (state == 0) then
local worldName = server.worldName()
io.open(local worldName".txt", "r")
io.output(file)
time = (file:read())
io.close(file)
end

It appears that you want to concatenate (join) two strings io.open(local worldName".txt", "r"). You should use the concat operator (double dot) for that:
local s1 = "Hello "
local s2 = "World!!!"
local s3 = s1..s2
print(s3)
This program will print the string "Hello world!!" (without the quotes).
Also, the you are only supposed to use the local keyword when declaring a variable.
So:
if (state == 1) then
local worldName = server.worldName()
io.open(worldName..".txt", "w")
io.output(file)
io.write(time)
io.close(file)
end
if (state == 0) then
local worldName = server.worldName()
io.open(worldName..".txt", "r")
io.output(file)
time = (file:read())
io.close(file)
end
This code is the correct version of what you wanted to write. I removed the incorrect uses of the local keyword and added the concat operator.

Related

Cannot change the boolean in table lua love2d

I am creating a simple platformer with love2d in lua.
I want the player to stand on the ground so that it won't fall.
However I got some proablem that I cannot change the string of a table return from player.lua
(It is a table because the player.lua return m and the last sentance, and m is a table)
I tested that the isOnFloor function is work, but I just can't change the boolean in the player table.
Main.lua
local love = require("love")
local tileMapper = require("tile/tileMapper")
local player = require "player"
local isOnFloor = require("isOnFloor")
function love.load()
love.window.setMode(1080, 640)
love.window.setTitle("Simple Platformer")
tileMapper:spawn()
tiles = tileMapper.tiles
player:setValue()
love.keyboard.keyPressed = {}
end
function love.update(dt)
for i = 1, #tiles do
if isOnFloor(player.x, player.y, tiles[i].x, tiles[i].y, 32, 32 * 3, 72, 72) then
player.currentState = "ground" -- I wanna change it here but when I go back to
--player.lua and try to let it out put the self.currentState, nothing changed --
else
player.currentState = "air"
end
end
player:update(dt)
love.keyboard.keyPressed = {}
end
I try to print out the player.currentState the the line after I set the player.currentState = "ground". It printed out "ground"
But if I print it in player.lua, it is always "air"
player.lua(I didn't paste some of the code here)
m = {}
function m:setValue()
self.x = 400
self.y = 50
self.vtx = 0
self.y_input = 0
self.speed = 300
self.jumpForce = -800
self.gravity = 100
self.anim = {idle = {img = love.graphics.newImage("assets/Idle.png"), maxFrame = 10},
run = {img = love.graphics.newImage("assets/Run.png"), maxFrame = 11},
jump = {img = love.graphics.newImage("assets/Jump.png"), maxFrame = 0},
fall = {img = love.graphics.newImage("assets/Fall.png"), maxFrame = 0}}
self.currentAnim = nil
for i = 1, #self.anim do
self.anim[i]:setFilter("nearest", "nearest")
end
self.frame = 0
self.timer = 0
self.currentState = nil
end
function m:update(dt)
print(self.currentState)
---------------------- Player States Update ------------------------
self:getXInput()
if self.currentState == "ground" then
if self.vtx == 0 then
self.currentAnim = self.anim.idle
elseif not(self.vtx == 0) then
self.currentAnim = self.anim.run
end
elseif self.currentState == "air" then
self.y_input = self.y_input + self.gravity
if self.y_input > 0 then
self.currentAnim = self.anim.fall
elseif self.y_input < 0 then
self.currentAnim = self.anim.jump
end
end
------------------------ Anim --------------------
self:animUpdate(dt)
if love.keyboard.keyPressed["space"] == true then
self.y_input = self.jumpForce
end
self.x = self.x + self.speed * dt * self.vtx
self.y = self.y + self.y_input * dt
end
return m
If you have an idea of this porblem, please tell, thank you.

Lua script sometimes doesn't save values in the file

I made a Timer script for Minecraft that should display the time spent in the game. You can pause resume and reset the time. My next step was to make it able to save the time in a file called timesave.txt but sometimes it isn't saving the time and its just empty... If it saves the time it reads and uses it to restore the old time.
This is the code:
name = "Timer"
description = "Just a normal Timer."
positionX = 0
positionY = 0
sizeX = 24
sizeY = 10
scale = 1
START_STOP_KEY = 0x55 --or 'U'
RESET_KEY = 0x4A --or 'J'
--
--[[
Timer Module Script by SebyGHG original script by Onix64(Stopwatch)
if you wish to change the key you can take the key code from here
https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
]] -------------script-code-------------
timesaved = 0
stopTime = 0
startTime = 0
f = io.input("timesave.txt")
resultstop = f :read("*line")
resultstart = f :read("*line")
f :close()
startTime = resultstop
stopTime = resultstart
timesaved = resultstart - resultstop
state = 2
function keyboard(key, isDown)
if (isDown == true) then
if (key == RESET_KEY) then
f = io.input("timesave.txt")
resultstart = f :read("*line")
resultstop = f :read("*line")
f :close()
startTime = resultstop
stopTime = resultstart
timesaved = resultstart - resultstop
state = 0
elseif (key == START_STOP_KEY) then
if (state == 0) then
state = 1
startTime = os.time() - timesaved
io.output("timesave.txt")
timesave= (io.open("timesave.txt","w"))
io.write(startTime,'\n',stopTime)
io.close(timesave)
elseif (state == 1) then
state = 2
stopTime = os.time()
elseif (state == 2) then
state = 1
startTime =startTime + os.time() - stopTime
end
end
end
end
TimerText = "00:00"
TextColor = {r = 30, g = 255, b = 30, a = 255}
function doubleDigit(number)
if (number < 10) then
return "0" .. math.floor(number)
else
return math.floor(number)
end
end
function timeText(time)
local result = ""
local days = 0
while (time > 86399) do
days = days + 1
time = time - 86400
end
local hours = 0
while (time > 3599) do
hours = hours + 1
time = time - 3600
end
local minutes = 0
while (time > 59) do
minutes = minutes + 1
time = time - 60
end
if (days == 0) then
if (hours == 0) then
return doubleDigit(minutes) .. ":" .. doubleDigit(time)
else
return math.floor(hours) .. " : " .. doubleDigit(minutes) .. ":" .. doubleDigit(time)
end
else
return math.floor(days) ..
" : " .. doubleDigit(hours) .. " : " .. doubleDigit(minutes) .. ":" .. doubleDigit(time)
end
end
function update()
if (state == 0) then
TextColor = {r = 255, g = 0, b = 0, a = 255}
TimerText = "00:00"
elseif (state == 1) then
TimerText = timeText(os.time() - startTime)
TextColor = {r = 0, g = 255, b = 255, a = 255}
elseif (state == 2) then
TimerText = timeText(stopTime - startTime)
TextColor = {r = 255, g = 255, b = 0, a = 255}
end
end
function render()
local font = gui.font()
local tw = font.width(TimerText)
gfx.color(0, 0, 0, 0)
gfx.rect(0, 0, tw + 4, 10)
gfx.color(TextColor.r, TextColor.g, TextColor.b, TextColor.a)
gfx.text(2, 1, TimerText)
end
Add io.flush() or timesave:flush() after your write operation to save the written data to the file.
See https://www.lua.org/manual/5.3/manual.html#pdf-file:flush

I am making a timer script in lua but if i stop it the time freezes and jumps forward when i resume it (Minecraft)

When I stop the timer and start it again, it jumps forward depending on how much time has passed when it was paused.
Link for a video of the problem: https://drive.google.com/file/d/1Xohbzbx4oyVfbRnSMLc_7u1gf7AWMJVy/view?usp=sharing
Code:
name = "Stopwatch"
description = "Count time for whatever reason..."
positionX = 0
positionY = 0
sizeX = 24
sizeY = 10
scale = 1
START_STOP_KEY = 0x55 --or 'U'
RESET_KEY = 0x4A --or 'I'
--[[
Stopwatch Module Script
if you wish to change the key you can take the key code from here
https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
]]--
-------------script-code-------------
previoustime = 0
state = 0
startTime = 0
stopTime = 0
function keyboard(key, isDown)
if (isDown == true) then
if (key == RESET_KEY) then
state = 0
end
end
end
function keyboard(key, isDown)
if (isDown == true) then
if (key == START_STOP_KEY) then
if (state == 0) then
state = 1
startTime = os.time()
elseif (state == 1) then
state = 2
previoustime = os.time()
stopTime = os.time()
elseif (state == 2) then
state = 1
time = previoustime
end
end
end
end
TimerText = "00:00"
TextColor = {r=30,g=255,b=30,a=255}
function doubleDigit(number)
if (number < 10) then
return "0" .. math.floor(number)
else
return math.floor(number)
end
end
function timeText(time)
local result = ""
local days = 0
while (time > 86399) do
days = days + 1
time = time - 86400
end
local hours = 0
while (time > 3599) do
hours = hours + 1
time = time - 86400
end
local minutes = 0
while (time > 59) do
minutes = minutes + 1
time = time - 60
end
if (days == 0) then
if (hours == 0) then
return doubleDigit(minutes) .. ":" .. doubleDigit(time)
else
return math.floor(hours) .. " : " .. doubleDigit(minutes) .. ":" ..doubleDigit(time)
end
else
return math.floor(days) .. " : " .. doubleDigit(hours) .. " : " .. doubleDigit(minutes) .. ":" .. doubleDigit(time)
end
end
function update()
if (state == 0) then
TextColor = {r=30,g=255,b=30,a=255}
TimerText = "00:00"
elseif (state == 1) then
TimerText = timeText(os.time() - startTime)
TextColor = {r=30,g=255,b=30,a=255}
elseif (state == 2) then
TimerText = timeText(stopTime - startTime)
TextColor = {r=30,g=30,b=255,a=255}
end
end
function render()
local font = gui.font()
local tw = font.width(TimerText)
gfx.color(0,0,0,120)
gfx.rect(0, 0, tw + 4, 10)
gfx.color(TextColor.r, TextColor.g, TextColor.b, TextColor.a)
gfx.text(2, 1, TimerText)
end
Thanks for helping!

Check Winner Naughts and Crosses game - Lua/Corona

I am quite new to the Lua language and am trying to create a simple Tic Tac Toe game. I am having difficulties figuring out how to check the winner. I have tried if-then statements but I think I am using them wrong. Not sure what the best way to check the winner is so any suggestions or assistance is greatly appreciated.
The code is -
local composer = require( "composer" )
local scene = composer.newScene()
d = display
w20 = d.contentWidth * .2
h20 = d.contentHeight * .2
w40 = d.contentWidth * .4
h40 = d.contentHeight * .4
w60 = d.contentWidth * .6
h60 = d.contentHeight * .6
w80 = d.contentWidth * .8
h80 = d.contentHeight * .8
----DRAW LINES FOR BOARD
local lline = d.newLine(w40,h20,w40,h80 )
lline.strokeWidth = 5
local rline = d.newLine(w60,h20,w60,h80 )
rline.strokeWidth = 5
local bline = d.newLine(w20,h40,w80,h40 )
bline.strokeWidth = 5
local tline = d.newLine(w20,h60,w80,h60 )
tline.strokeWidth = 5
--PLACE BOARD COMPARTMENT DIMENSIONS IN TABLE
board ={
{"tl",1,w20,h40,w40,h20,0},
{"tm",2,w40,h40,w60,h20,0},
{"tr",3,w60,h40,w80,h20,0},
{"ml",4,w20,h60,w40,h40,0},
{"mm",5,w40,h60,w60,h40,0},
{"mr",6,w60,h60,w80,h40,0},
{"bl",7,w20,h80,w40,h60,0},
{"bm",8,w40,h80,w60,h60,0},
{"br",9,w60,h80,w80,h60,0}
}
--
local EMPTY, X, O = 0, 1, 2
local whichTurn = 0
--FILL COMPARTMENT W/ COLOUR WHEN TOUCHED
local function fill(event)
if (event.phase == "ended") then
for t = 1,9 do
if event.x > board[t][3] and event.x < board[t][5] then
if event.y < board[t][4] and event.y > board[t][6] then
if board[t][7] == EMPTY then
if whichTurn == 1 then
whichTurn = 2
else
whichTurn = 1
end
board[t][7] = whichTurn
if board[t][7] == 1 then
local xText = display.newText("X", board[t][3], board[t][4], "Arial", 80)
xText.anchorX = 0
xText.anchorY = 100
elseif board[t][7] == 2 then
local oText = display.newText("O", board[t][3], board[t][4], "Arial", 80)
oText.anchorX = 0
oText.anchorY = 100
end
end
end
end
end
end
local function checkWinner()
for i = 1,9 do
if board[i][2] == 1 and board[i][7] == 1 then
boxOne = "x"
end
if board[i][2] == 2 and board[i][7] == 1 then
boxTwo = "x"
end
if board[i][2] == 3 and board[i][7] == 1 then
boxThree = "x"
end
if boxOne == "x" and boxTwo == "x" and boxThree == "x" then
display.newText("Winner", 10, 100, "Arial", 200)
end
end
end
end
Runtime:addEventListener ("touch", fill)
return scene
local All_Lines = {{1,5,9}, {3,5,7}, {1,2,3}, {4,5,6}, {7,8,9}, {1,4,7}, {2,5,8}, {3,6,9}}
local function checkWinner()
for _, Line in ipairs(All_Lines) do
local values = 0
for _, idx in ipairs(Line) do
values = values * 10 + board[idx][7]
end
if values == 111 then
display.newText("Winner", 10, 100, "Arial", 200)
return
elseif values == 222 then
display.newText("Loser", 10, 100, "Arial", 200)
return
end
end
end

Is there a way to add a numerical value to a variable in lua in a for loop?

I made this program:
a = math.random(0, 9) - 0 -- In this stage, if the result equals zero, that means it's a match
b = math.random(0, 9) - 1
c = math.random(0, 9) - 2
d = math.random(0, 9) - 3
e = math.random(0, 9) - 4
f = math.random(0, 9) - 5
g = math.random(0, 9) - 6
h = math.random(0, 9) - 7
i = math.random(0, 9) - 8
j = math.random(0, 9) - 9
print("Enter the number of trials you want to simulate.") -- This is where I decide how many trials I want to do
var = io.read()
a_ = 0 -- This is where I hope to keep the number of "a" matches, number of "b" matches, etc. The frequency
b_ = 0
c_ = 0
d_ = 0
e_ = 0
f_ = 0
g_ = 0
h_ = 0
i_ = 0
j_ = 0
for k = 1, var do -- One loop is a trial
print("Trail #"..k)
if a == 0 then
print("a = match")
elseif a ~= 0 then
print("a = not a match")
end
if b == 0 then
print("b = match")
elseif b ~= 0 then
print("b = not a match")
end
if c == 0 then
print("c = match")
elseif c ~= 0 then
print("c = not a match")
end
if d == 0 then
print("d = match")
elseif d ~= 0 then
print("d = not a match")
end
if e == 0 then
print("e = match")
elseif e ~= 0 then
print("e = not a match")
end
if f == 0 then
print("f = match")
elseif f ~= 0 then
print("f = not a match")
end
if g == 0 then
print("g = match")
elseif g ~= 0 then
print("g = not a match")
end
if h == 0 then
print("h = match")
elseif h ~= 0 then
print("h = not a match")
end
if i == 0 then
print("i = match")
elseif i ~= 0 then
print("i = not a match")
end
if j == 0 then
print("j = match")
elseif j ~= 0 then
print("j = not a match")
end
end
while true do --This is just to keep the window open after the program is done so that I can observe the data, you can ignore this
end
As you can see, I tried to add one to a_, b_ and c_ every time it returns a result of zero, but it doesn't work, it there a way to do this?
The reason I want to do this is for an AP stats class I'm taking, and this will make it a lot easier to do. I'm just doing a_, b_, c_ for now, once I solve this issue, I'll do all of them. Thanks for reading!
Assuming you want the simulation to run 'var' times, try this:
math.randomseed(os.time())
local matchStorage = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
local randomNums = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
local function runSimulation(numTrial)
print("\nRunning trial #: " .. numTrial)
for index, value in pairs(randomNums) do
local variable = math.random(0, 9)
randomNums[index] = variable
end
for index, value in pairs(randomNums) do
if randomNums[index] == 0 then
matchStorage[index] = matchStorage[index] + 1
print("index " .. index .. " is a match.")
else
print("index " .. index .. " is not a match.")
end
end
end
do
print("Enter the number of trials")
numTrials = io.read()
for index = 1, numTrials do
runSimulation(index)
end
print("\nRESULTS:\n")
for index, value in pairs(matchStorage) do
print("index " .. index .. " frequency: " .. value)
end
end
The number of times that one of the following 'randomNum' values contain a 0 will be stored in its corresponding 'matchStorage' index.

Resources