Roblox Studio - Each block doesn't have the same value - lua

As you can see on this photo: https://gyazo.com/9ca355a460c5602ec073bcbae701dede i clicked on every block. My problem is that every block doens't have the same Value. I want every block when i click at it to go down 1/10 everytime i click and still give me 1 in my backpack pr. click. Right now i can see that the value of the blocks if not the same.
Here is the script for the blocks: https://gyazo.com/b627ae8559b9035f0fdfe88297b13364 (Think the problem is here)
As you can see on this photo the value is 0,8. And the other one downstairs is 0,1 and so on https://gyazo.com/ac5359b7f6a7e123ad31507f4384323d
The damage value is 0.2 as u see here: https://gyazo.com/b9de1795c27eb151caf8db2d79e9f58a
Here is the script for the shovel:
https://gyazo.com/14b5e675be5644056bda8b24f828ec6a
I would appreciate if u could help me to fix this problem. Tell me if u need more information about this to fix this problem. Hope u understand my problem.

In this line (26) u r generating a random damage value
dmg.value = math.random(1, 10)/10
Did u try to set it on
dmg.value = 1/10
?

Related

Is there a reason why this code is not outputting a worth

Problem
Hello, StackOverflow community! I am working on this Lua game, and I was testing to see if it would change the text on my TextLabel to the Bitcoins current worth, I was utterly disappointed when nothing showed up.
I have tried to do research on Google, and my code seems to be just right.
Code
Change = false
updated = false
while Change[true] do --While change = true do
worth = math.random(1,4500) --Pick random number
print('Working!') --Say its working
Updated = true --Change the updated local var.
end --Ending while loop
script.Parent.TextLabel.Text.Text = 'Bitcoin is currently worth: ' .. worth
--Going to the Text, and changing in to a New worth.
while Updated[false] do --While updated = false do
wait(180) --Wait
Change = true --After waits 3 minutes it makes an event trigger
end -- Ending while loop
wait(180) --Wait
Updated = false --Reseting Script.
I expect the output on the Label to be a random number.
I can't really speak to roblox, but there are a couple of obvious problems with your code:
Case
You have confusion between capitalized ("Updated", "Change") and lowercase ("updated", "change" [in commented while statement]), which will fail. See, for example:
bj#bj-lt:~$ lua
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
> Updated = true
> print(Updated)
true
> print(updated)
nil
So be super-careful about what identifiers you capitalize. In general, most programmers leave variables like that in all-lowercase (or sometimes things like camelCase). I suppose there might be some oddball lua runtime out there that is case-insensitive, but I don't know of one.
Type misuse.
Updated is a boolean (a true/false value), so the syntax:
while Change[true] do
...is invalid. See:
> if Updated[true] then
>> print("foo")
>> end
stdin:1: attempt to index global 'Updated' (a boolean value)
stack traceback:
stdin:1: in main chunk
[C]: in ?
Note also that the "While change == true do" is also wrong because of case ("While" is not valid lua, but "while" is).
Lastly:
Lack of threading.
You have basically two different things that you're trying to do at once, namely randomly change the "worth" variable as fast as possible (it's in a loop) and see a set a label to match it (it looks like you probably want it to change constantly). This requires two threads of operation (one to change worth and another to read it and stick it on the label). You've written this like you're assuming you have a spreadsheet or something and that. What your code is actually doing is:
Setting some variables
Updating worth indefinitely, printing 'Working!' a bunch, and...
Never stopping
The rest of the code never runs, because the rest of the code isn't in a background thread (basically the first bit monopolizes the runtime and never yields to everything else).
Lastly, even if the top code was running in the background, you only set the Text label one-time to exactly "Bitcoin is currently worth: 3456" (or some similar number) one time. The way this is written there won't be any updates thereafter (and, if it runs once before the other thread has warmed up, it might not be set to anything useful at all).
My guess is that your runtime is spitting out errors left and right due to the identifier problems and/or is running in a tight infinite loop and never actually getting to the label refresh logic.
BJ Black has given an excellent description of the issues with the syntax, so I'll try to cover the Roblox piece of this. In order for this kind of thing to work properly in a Roblox game, here are some assumptions to double check :
Since we are working with a TextLabel, is it inside a ScreenGui? Or a SurfaceGui?
If it's in a ScreenGui, make sure that ScreenGui is in StarterGui, and is this code in a LocalScript
If it's in a SurfaceGui, make sure that SurfaceGui is adorning a Part and this code
is in a Script
After you checked all those pieces, maybe this is closer to what you were thinking :
-- define the variables we're working with
local textLabel = script.Parent.TextLabel
local worth = 0
-- create an infinite loop
spawn(function()
while true do
--Pick random number
worth = math.random(1,4500)
-- update the text of the label with the new worth
textLabel.Text = string.format("Bitcoin is currently worth: %d", worth)
-- wait for 3 minutes, then loop
wait(180)
end
end)
I removed Updated and Changed because all they were doing was deciding whether or not to change the value. The flow of your loop was:
do nothing and display an undefined number. Wait 3 minutes
update the number, display it, wait 6 minutes
repeat 1 and 2.
So hopefully this is a little clearer and closer to what you were thinking.

FCEUX stops responding when i run this code made by me

--llama a IUP
require("iuplua")
--Variables
on= 1
puntos1=memory.readbyte(0x0007DE) --Read the points value
puntos2=memory.readbyte(0x0007DF)
puntos3=memory.readbyte(0x0007E0)
puntos4=memory.readbyte(0x0007E1)
puntos5=memory.readbyte(0x0007E2)
p1=puntos1*100000 --Convert the raw values to it's game value
p2=puntos2*10000
p3=puntos3*1000
p4=puntos4*100
p5=puntos5*10
maxpuntaje=p1+p2+p3+p4+p5 --Calculate the final result
mundo=memory.readbyte(0x00075F) --Read the "world" value
nivel=memory.readbyte(0x000760) --Read the "level" value
estado=memory.readbyte(0x000770) --Read the mario "state" (00 not in game, 01 playing, 03 game over)
--Escribir las variables anteriores al morir
memory.writebyte(0x00075A,00) --Change the "lives" value to 1
while(on==1) do
estado=memory.readbyte(0x000770)
print(estado)
print(type(estado))
if(estado==03)then
print("Puntuacion maxima (sesion actual) = ",maxpuntaje)
print("Mundo y nivel Actual: ",mundo+1,"-",nivel+1)
end
end
(code edited)
This is the state of 0x000770 when playing
This is the state of 0x000770 when the Game Over scene jumps in
is there anything wrong? fceux just stop responding when i run this script, im new at stacks overflow and at programming so, every help will be welcome
It's rather simple to debug this but more information is required
Make sure (estado==03) yields true and also print following variables and add into your question
mundo=memory.readbyte(0x00075F)
nivel=memory.readbyte(0x000760)
estado=memory.readbyte(0x000770)
print(mundo, nivel, estado)
Check the types of these memory read vaiables i-g print(type(mundo))
Make Sure that the if condition checks against correct types
You realize that memonry is read once and while is always true
Try this:
while(on==1) do
estado=memory.readbyte(0x000770)
print(estado)
print(type(estado))
if(estado==03)then
print("Puntuacion maxima (sesion actual) = ",maxpuntaje)
print("Mundo y nivel Actual: ",mundo+1,"-",nivel+1)
end
end
im so sorry for wasting your time, but i found the error, it was actually quite simple, there was a missing FCEU.frameadvance at the end of the cycle.
i dont know why it caused the emulator to crash but after putting it in place it stopped crashing. thanks to wsha for helping.

Logistic Regression - loss function

I tried to substitute p = 1/1+e(-z) back to the first function, but I can't reach the simplify function.
For the middle term, I get the sum of (z(1-t^i)) instead of the sum of (t^i z^i). I don't know what's going wrong.
Since you have not posted your solution,I cannot say what mistake you did commit but I think you need to substitute p(C=0|x,w) and p(C=1|x,w) both and not just p=1/1+e(-z).Here goes the derivation-
substitute p(C=0|x,w) and p(C=1|x,w) in main equation(first)(sum==summation) and z is z^(i)
L=-sum(t^(i)*log(e^-z))+sum(t^(i)*log(1+e^-z))+sum(log(1+e^-z)-sum(t^(i)*log(1+e^-z))
The second and fourth term cancel out.We get
L=-sum(t^(i)*log(e^-z))+sum(log(1+e^-z)
By prop of logarithms-log(e^(-z))=z.So
L=sum(log(1+e^-z)+sum(t^(i)*z)

In Lua, how do I prevent a program from crashing, after a user left an value blank, which the program needs to calculate?

I wanted to create a program for me and friends, which calculates a value.
However I also wanted to make the program uncrashable, so my friends dont tell me how they broke my code.
So the problem is that they could leave this: d = io.read()
blank resulting the program to crash once it wants to calculate d.
So how do I stop them from leaving d blank or entering a string?
You can make it so it defaults to 0
local d = tonumber(io.read()) or 0

Can't modify loop-variable in lua [duplicate]

This question already has answers here:
Lua for loop reduce i? Weird behavior [duplicate]
(3 answers)
Closed 7 years ago.
im trying this in lua:
for i = 1, 10,1 do
print(i)
i = i+2
end
I would expect the following output:
1,4,7,10
However, it seems like i is getting not affected, so it gives me:
1,2,3,4,5,6,7,8,9,10
Can someone tell my a bit about the background concept and what is the right way to modify the counter variable?
As Colonel Thirty Two said, there is no way to modify a loop variable in Lua. Or rather more to the point, the loop counter in Lua is hidden from you. The variable i in your case is merely a copy of the counter's current value. So changing it does nothing; it will be overwritten by the actual hidden counter every time the loop cycles.
When you write a for loop in Lua, it always means exactly what it says. This is good, since it makes it abundantly clear when you're doing looping over a fixed sequence (whether a count or a set of data) and when you're doing something more complicated.
for is for fixed loops; if you want dynamic looping, you must use a while loop. That way, the reader of the code is aware that looping is not fixed; that it's under your control.
When using a Numeric for loop, you can change the increment by the third value, in your example you set it to 1.
To see what I mean:
for i = 1,10,3 do
print(i)
end
However this isn't always a practical solution, because often times you'll only want to modify the loop variable under specific conditions. When you wish to do this, you can use a while loop (or if you want your code to run at least once, a repeat loop):
local i = 1
while i < 10 do
print(i)
i = i + 1
end
Using a while loop you have full control over the condition, and any variables (be they global or upvalues).
All answers / comments so far only suggested while loops; here's two more ways of working around this problem:
If you always have the same step size, which just isn't 1, you can explicitly give the step size as in for i =start,end,stepdo … end, e.g. for i = 1, 10, 3 do … or for i = 10, 1, -1 do …. If you need varying step sizes, that won't work.
A "problem" with while-loops is that you always have to manually increment your counter and forgetting this in a sub-branch easily leads to infinite loops. I've seen the following pattern a few times:
local diff = 0
for i = 1, n do
i = i+diff
if i > n then break end
-- code here
-- and to change i for the next round, do something like
if some_condition then
diff = diff + 1 -- skip 1 forward
end
end
This way, you cannot forget incrementing i, and you still have the adjusted i available in your code. The deltas are also kept in a separate variable, so scanning this for bugs is relatively easy. (i autoincrements so must work, any assignment to i below the loop body's first line is an error, check whether you are/n't assigning diff, check branches, …)

Resources