Roblox Lua - Color3 expected, got string - lua

local colorTable = {
('11, 13, 48'),
('35, 48, 48'),
('6, 47, 6'),
('91, 5, 7')
}
local value = math.random(1,#colorTable)
local picked_value = colorTable[value]
script.Parent.Background.BackgroundColor3 = picked_value
My error code was Color3 expected, got string, is there anything I can fix this? My goal was simple, randomizing a specific color rgb to a frame backrgound.

A Color3 object can be created by Color3.fromRGB.
local colorTable = {
Color3.fromRGB(11, 13, 48),
...
}

Related

I can't find what makes the error, does anyone know how to fix it?

I was following a tutorial and did exactly what it showed but it doesn't work and I can't figure out why.
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
local sheetOptions = {
width = 512,
height = 256,
numFrames = 8
}
local sheet_runningCat = graphics.newImageSheet( "sprites-cat-running.png", sheetOptions )
local sequences_runningCat = {
{
name = "normalRun",
start = 1,
count = 8,
time = 800,
loopCount = 0,
loopDirection = "forward"
}
}
local runningCat = display.newSprite( sheet_runningCat, sequences_runningCat )
It creates this error...
main.lua:26: bad argument #1 to 'newSprite' (ImageSheet expected, got nil)
stack traceback:
[C]: in function 'newSprite'
main.lua:26 in main chunk
Do you want to relaunch the project?
It means that the file "sprites-cat-running.png" was not found in the same directory as this file main.lua so graphics.newImageSheet() returns a nil value, and sheet_runningCat is now nil instead of an ImageSheet object.
To fix this, download the image file from the tutorial and save it in the same directory as main.lua

LUA and table data - finding data in a nested loop

I have the following table
scavenging =
{
{
type = "Greenskin|16",
fast_levelup = 20, --Number of levels with 75% chance to level up after required level
normal_levelup = 40, --Number of levels with 50% chance to level up after fast_levelup + required level
slow_levelup = 40, --Number of levels with 25% chance
drops = --Drops
{
{items = {"Linen", "Bolt of Linen", "Coarse Thread", "Feather", "Cotton"}, droprates = {60, 10, 10, 10, 2}},
},
},
}
This is one data value in a series. I use
function scavenge_meta(scavenge_name)
for _, meta in pairs(scavenging) do
if string.match(meta.type, scavenge_name) then
return meta
end
end
end
to pull the needed data. The question is, is there an easy way to get to the droprates value without having to do a few for (pairs)? Right now for example I can use:
local founditem = scavenge_meta("Greenskin|16")
And this works, and then I can use founditem.fast_levelup etc. I was hoping to access the drops table with founditem.drops.items for example, but this doesn't work, I need to do a pairs(founditem.drops) then pairs(valuefound.items) /etc.
Maybe there is a better way of doing this?
if you can change the table then do:
scavenging =
{
["Greenskin|16"] = {
type = "Greenskin|16",
fast_levelup = 20,
normal_levelup = 40,
slow_levelup = 40,
drops =
{
{items = {"Linen", "Bolt of Linen", "Coarse Thread", "Feather", "Cotton"}, droprates = {60, 10, 10, 10, 2}},
},
},
}
and get data directly by index
print(scavenging["Greenskin|16"].fast_levelup)
otherwise, just by searching as you did.

AudioKit 4.0 AKPeriodicFunctions Not Starting

Unable to start AK4 AKPeriodic function in XCode 9. In the following excerpt from the "Plucked String" playground, I inserted two log messages to help identify processing events. I never see the "periodic function startup" message in the console log. All I hear is a very short clicking sound.
let scale = [0, 2, 4, 5, 7, 9, 11, 12]
let performance = AKPeriodicFunction(frequency: playRate) {
print("periodic function startup")
var note = scale.randomElement()
let octave = [2, 3, 4, 5].randomElement() * 12
if random(0, 10) < 1.0 { note += 1 }
if !scale.contains(note % 12) { print("ACCIDENT!") }
let frequency = (note + octave).midiNoteToFrequency()
if random(0, 6) > 1.0 {
pluckedString.trigger(frequency: frequency)
}
}
AudioKit.output = reverb
AudioKit.start(withPeriodicFunctions: performance)
print("AK startup")
performance.start()
This is fixed in the develop branch, as per this issue:
https://github.com/AudioKit/AudioKit/issues/1066

Word wrap in generated PDF (using jsPDF)?

what I'm doing is using jsPDF to create a PDF of the graph I generated. However, I am not sure how to wrap the title (added by using the text() function). The length of the title will vary from graph to graph. Currently, my titles are running off the page. Any help would be appreciated!
This is the code i have so far:
var doc = new jsPDF();
doc.setFontSize(18);
doc.text(15, 15, reportTitle);
doc.addImage(outputURL, 'JPEG', 15, 40, 180, 100);
doc.save(reportTitle);
Nothing to keep the reportTitle from running off the page
Okay I've solved this. I used the jsPDF function, splitTextToSize(text, maxlen, options). This function returns an array of strings. Fortunately, the jsPDF text() function, which is used to write to the document, accepts both strings and arrays of strings.
var splitTitle = doc.splitTextToSize(reportTitle, 180);
doc.text(15, 20, splitTitle);
You can just use the optional argument maxWidth from the text function.
doc.text(15, 15, reportTitle, { maxWidth: 40 });
That will split the text once it reaches the maxWidth and start on the next line.
Auto-paging and text wrap issue in JSPDF can achieve with following code
var splitTitle = doc.splitTextToSize($('#textarea').val(), 270);
var pageHeight = doc.internal.pageSize.height;
doc.setFontType("normal");
doc.setFontSize("11");
var y = 7;
for (var i = 0; i < splitTitle.length; i++) {
if (y > 280) {
y = 10;
doc.addPage();
}
doc.text(15, y, splitTitle[i]);
y = y + 7;
}
doc.save('my.pdf');
To wrap long string of text to page use this code:
var line = 25 // Line height to start text at
var lineHeight = 5
var leftMargin = 20
var wrapWidth = 180
var longString = 'Long text string goes here'
var splitText = doc.splitTextToSize(longString, wrapWidth)
for (var i = 0, length = splitText.length; i < length; i++) {
// loop thru each line and increase
doc.text(splitText[i], leftMargin, line)
line = lineHeight + line
}
If you need to dynamically add new lines you want to access the array returned by doc.splitTextToSize and then add more vertical space as you go through each line:
var y = 0, lengthOfPage = 500, text = [a bunch of text elements];
//looping thru each text item
for(var i = 0, textlength = text.length ; i < textlength ; i++) {
var splitTitle = doc.splitTextToSize(text[i], lengthOfPage);
//loop thru each line and output while increasing the vertical space
for(var c = 0, stlength = splitTitle.length ; c < stlength ; c++){
doc.text(y, 20, splitTitle[c]);
y = y + 10;
}
}
Working Helper function
Here's a complete helper function based on the answers by #KB1788 and #user3749946:
It includes line wrap, page wrap, and some styling control:
(Gist available here)
function addWrappedText({text, textWidth, doc, fontSize = 10, fontType = 'normal', lineSpacing = 7, xPosition = 10, initialYPosition = 10, pageWrapInitialYPosition = 10}) {
var textLines = doc.splitTextToSize(text, textWidth); // Split the text into lines
var pageHeight = doc.internal.pageSize.height; // Get page height, well use this for auto-paging
doc.setFontType(fontType);
doc.setFontSize(fontSize);
var cursorY = initialYPosition;
textLines.forEach(lineText => {
if (cursorY > pageHeight) { // Auto-paging
doc.addPage();
cursorY = pageWrapInitialYPosition;
}
doc.text(xPosition, cursorY, lineText);
cursorY += lineSpacing;
})
}
Usage
// All values are jsPDF global units (default unit type is `px`)
const doc = new jsPDF();
addWrappedText({
text: "'Twas brillig, and the slithy toves...", // Put a really long string here
textWidth: 220,
doc,
// Optional
fontSize: '12',
fontType: 'normal',
lineSpacing: 7, // Space between lines
xPosition: 10, // Text offset from left of document
initialYPosition: 30, // Initial offset from top of document; set based on prior objects in document
pageWrapInitialYPosition: 10 // Initial offset from top of document when page-wrapping
});
When we use linebreak in jsPDF we get an error stating b.match is not defined, to solve this error just unminify the js and replace b.match with String(b).match and u will get this error twice just replace both and then we get c.split is not defined just do the same in this case replace it with String(c).match and we are done. Now you can see line breaks in you pdf. Thank you

Lua - Not displaying text in Corona SDK

In this app I'm creating with Corona SDK, when you win the text "You win" sould appear, but doesn't. I could post all the code, but I don't think the rest would be hepful, so here is only the essencial:
_H = display.contentHeight;
_W = display.contentWidth;
mRand = math.random;
o = 0;
time_remain = 20;
time_up = false;
total_orbs = 45;
total_secs = 20;
ready = false;
local backGround = display.newImage("media/bg.png");
backGround.xScale = 2;
backGround.yScale = 2;
loseMSG = display.newText("You Lose!", _W/2, _H/2, nil, 50)
loseMSG.isVisible = false
winMSG = display.newText("You Win!", _W/2, _H/2, nil, 50)
winMSG.isVisible = false
local countdowntxt = display.newText(time_remain, 0, 0, native.systemFont, 60);
countdowntxt.xScale = .5; countdowntxt.yScale = .5;
countdowntxt:setReferencePoint(display.BottomRightReferencePoint);
countdowntxt.x = _W-20; display.y = _H-20;
countdowntxt:setTextColor(0, 0, 0)
function winLose(condition)
if (condition == "Win") then
bgAlpha = display.newImage("media/bgAlpha.png");
bgAlpha.xScale = 2;
bgAlpha.yScale = 2;
winMSG.isVisible = true -- Here the win text should become visible, but doesn't
elseif (condition == "Fail") then
bgAlpha = display.newImage("media/bgAlpha.png");
bgAlpha.xScale = 2;
bgAlpha.yScale = 2;
loseMSG.isVisible = true
end
end
Any Ideas why?
You need to take a divide and conquer approach.
Does this work?
winMSG = display.newText("You Win!", _W/2, _H/2, nil, 50)
winMSG.isVisible = true <-- note this is set to true
It does?
Does winLose every get called? Put a print statement in it or a break point or whatever you use on your platform to debug.
It does?
What does the variable condition contain? Inspect it/print it out and verify that it is indeed "Win", same case, no spaces, etc. or you can put a print statement in the appropriate branch of that function to make sure it's being hit.
Is that OK?
Does it show up if you remove the bgAlpha code?
So on and so forth.
I don't know Corona, but Googling the docs for newText it's possible you have the parameters wrong (you're not passing a font).
I could post all the code
The less you post the better, because that means you've already gone through the steps shown above to try to isolate the problem. Nine times out of ten, doing that will reveal the problem all by itself.
Why have you given nil for font value?
Atleast pass the default system font
loseMSG = display.newText("You Lose!", _W/2, _H/2, native.systemFont, 50)
winMSG = display.newText("You Win!", _W/2, _H/2, native.systemFont, 50)

Resources