Tokio

[QUESTION] What wrong with this "len"?

Recommended Posts

i got this error, but the script working fine... -.- 
bad argument #1 to 'len' (string expected, got boolean)

how to fix this? :S 

code:

function currentSongName() 
local radio_title = getElementData(resourceRoot, "radio.title")
if string.len(radio_title) >= x*0.0625 then
radio_title = string.sub(radio_title, 1, 80)..".."
end
	for i, shading in pairs ( offsetShadings ) do
	dxDrawText("Radio: "..radio_title, xD+shading[1], yD+shading[2], xD, yD+textHeight, tocolor ( 0, 0, 0, 255 ), scale, font, "left", "center", false, false, true, true)
	end
	dxDrawText("Radio: #ffffff"..radio_title, xD, yD, xD, yD+textHeight, tocolor ( 70, 215, 0, 255 ), scale, font, "left", "center", false, false, true, true)
end
addEventHandler('onClientRender', root, currentSongName)

 

Share this post


Link to post
8 hours ago, 50cent said:

i got this error, but the script working fine... -.- 
bad argument #1 to 'len' (string expected, got boolean)

how to fix this? :S 

code:


function currentSongName() 
local radio_title = getElementData(resourceRoot, "radio.title")
if string.len(radio_title) >= x*0.0625 then
radio_title = string.sub(radio_title, 1, 80)..".."
end
	for i, shading in pairs ( offsetShadings ) do
	dxDrawText("Radio: "..radio_title, xD+shading[1], yD+shading[2], xD, yD+textHeight, tocolor ( 0, 0, 0, 255 ), scale, font, "left", "center", false, false, true, true)
	end
	dxDrawText("Radio: #ffffff"..radio_title, xD, yD, xD, yD+textHeight, tocolor ( 70, 215, 0, 255 ), scale, font, "left", "center", false, false, true, true)
end
addEventHandler('onClientRender', root, currentSongName)

 

 

I assume you get the error message on line 3 since you never said where, if so to avoid the error message just change line 2 to this

local radio_title = getElementData(resourceRoot, "radio.title") or ""

the reason you get that error message is because whenever "radio.title" doesn't exist it returns it as false, and you're trying to get the length of a string in which it gives you that error message.

I hope it helps, I can't confirm this solves the problem, as I've run into very weird problems with Lua lately, however, it should work script-wise.

  • Thanks 1

Share this post


Link to post
6 hours ago, Ben_Sherman said:

 

I assume you get the error message on line 3 since you never said where, if so to avoid the error message just change line 2 to this


local radio_title = getElementData(resourceRoot, "radio.title") or ""

the reason you get that error message is because whenever "radio.title" doesn't exist it returns it as false, and you're trying to get the length of a string in which it gives you that error message.

I hope it helps, I can't confirm this solves the problem, as I've run into very weird problems with Lua lately, however, it should work script-wise.

Thank you!:D

Share this post


Link to post
7 hours ago, Ben_Sherman said:

 

I assume you get the error message on line 3 since you never said where, if so to avoid the error message just change line 2 to this


local radio_title = getElementData(resourceRoot, "radio.title") or ""

the reason you get that error message is because whenever "radio.title" doesn't exist it returns it as false, and you're trying to get the length of a string in which it gives you that error message.

I hope it helps, I can't confirm this solves the problem, as I've run into very weird problems with Lua lately, however, it should work script-wise.

Why output the another player's music? O.o 

Share this post


Link to post
1 hour ago, 50cent said:

Why output the another player's music? O.o 

Because you are saving the elementData to the resource. Doing this means everyone will save the elementData to the same key in the resource overwriting the previous title.

I suggest saving the elementData to the player or if the radio has an object set it to the object/vehicle.

Share this post


Link to post
14 minutes ago, Mr.Loki said:

Because you are saving the elementData to the resource. Doing this means everyone will save the elementData to the same key in the resource overwriting the previous title.

I suggest saving the elementData to the player or if the radio has an object set it to the object/vehicle.

How?

Share this post


Link to post
On 2017-08-02 at 13:49, 50cent said:

How?

 

Not sure where you're saving it, but here is how you get the data from a player as well an example of storing it, however, the storing function is client-side ONLY. If you want to save the data from a server-side script you need to replace getLocalPlayer() with the player itself.

getElementData(getLocalPlayer(), "radio.title")
setElementData(getLocalPlayer(), "radio.title", theData)
Edited by Ben_Sherman
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.