'How could I add a skill point limiter in lua?
Ok so I'm building a Text-based adventure in lua... If anyone thinks that is a bad idea well I am the master of bad ideas but the question is about the level up function... So here it is
function lvl()
if xp >= xpr then
level = level + 1
xpr = xpr * 1.5
xp = 0
io.write("Congrats! Somehow you didn't die and lose everything!", "\n")
io.write("Your level is now:" .. level .. "\n")
sp = sp + 6
io.write("Do you want to allocate your " .. sp .. " skill points?", "\n")
sac = io.read()
if sac == "yes" then
io.write("How much do you want to put into strength?", "\n")
strsp = io.read()
if sp == 0 then
io.write("You don't have any skill points!", "\n")
else
str = str + strsp
io.write("Strength: ", str, "\n")
end
io.write("How much do you want to put into dexterity?", "\n")
dexsp = io.read()
if sp == 0 then
io.write("You don't have any skill points!", "\n")
else
dex = dex + dexsp
io.write("Dexterity: ", dex, "\n")
end
io.write("How much do you want to put into constitution?", "\n")
consp = io.read()
if sp == 0 then
io.write("You don't have any skill points!", "\n")
else
con = con + consp
io.write("Constitution: ", str, "\n")
end
io.write("How much do you want to put into intelligence?", "\n")
intsp = io.read()
if sp <= 0 then
io.write("You don't have any skill points!", "\n")
else
int = int + intsp
end
io.write("How much do you want to put into wisdom?", "\n")
wissp = io.read()
if sp <= 0 then
io.write("You don't have any skill points!", "\n")
else
wis = wis + wissp
end
io.write("How much do you want to put into charisma", "\n")
chasp = io.read()
if sp <= 0 then
io.write("You don't have any skill points!", "\n")
else
cha = cha + chasp
end
elseif xp < xpr then
io.write("You didn't level up!", "\n")
end
end
and the problem lies within the skill allocation system. I cannot figure out how to limit the amount of skill points you spend... Thank you in advance!
PS: I'm very new to lua.
Solution 1:[1]
Convert the string from io.read()
to a number...
io.write("How much do you want to put into strength?", "\n")
strsp = tonumber(io.read())
if sp < strsp then -- check input is not more than sp
io.write("You don't have enough skill points!", "\n")
else
sp = sp - strsp -- substract from sp
str = str + strsp -- add to str
io.write("Strength: ", str, "\n")
end
PS: Give user info about amount of sp
and current str
.
For example with format()
method...
io.write(("%s %d %s%d%s: "):format("How much from", sp, "do you want to put into strength? (", str, ")"))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |