Hello, I am looking for help to find & solve an error.
Having searched the documentation & finding nothing, I thought this might help some!
!1: --# Code | LocalScript --#
!2: function ChangedRO()
!3: RemoveOpen.Value = OOPetsBase + OOSacrificesBase + OOSettingsBase + OOStatsBase
!4: end
!5: OOPetsBase.Changed:Connect(ChangedRO)
!6: OOSacrificesBase.Changed:Connect(ChangedRO)
!7: SettingsBase.Changed:Connect(ChangedRO)
!8: StatsBase.Changed:Connect(ChangedRO)
--# (Here is the mistake) --#
!5: attempt to index number with 'Changed'
Thank you in advance for your answers!
You are getting this error because you are doing 5.Changed
Changed is a method of an instance, so make sure you do it on the instance not on the .Value e.g.
local intVal = Instance.new("IntValue")
intVal.Changed:Connect(...)
-- And not
local val = intVal.Value
val.Changed:Connect(...)
Can you post the the full code next time
Related
This question here seems to tangentially touch upon it but I cannot get it to work. Here is my LUA file:
function conky_myeval()
local myTable = { " Old London :normal:size=7", "Ethnocentric :normal:size=7"}
var1 = myTable[ math.random( #myTable)]
return var1
end
and the related conky part:
${font ${lua conky_myeval}} Hello World!
Thank you for any assistance and I apologize if this has been asked before; The most similar I found I posted above.
I've found it easier to have a lua script pass conky a string that can be parsed by a lua_parse object that then generates the intended object rather than trying to pass a value to the intended object.
In the case of random fonts, I'd do something like the following, which worked when tested.
Lua file:
function conky_myfont()
local myTable = {"DejaVu Serif:normal:size=12", "MuseJazz Text:normal:size=12"}
var1 = myTable[ math.random( #myTable)]
return "${font "..var1.."}"
end
Conky part:
${lua_parse conky_myfont}Hello World!${font}
local Player = game.Players.LocalPlayer
local PlayerCash = Player:WaitForChild("leaderstats"):WaitForChild("Strength")
local Mouse = Player:GetMouse()
local amount = 50
script.Parent.Activate:Connect(function()
game.Players.LocalPlayer.leaderstats.money.Value = game.Players.LocalPlayer.leaderstats.money.Value + amount
end)
I am trying to make a code to give Strength when you click. When I press 'Play' it doesn't work. All it says in the output is
'attempt to index function with 'Connect'.'
The error "attempt to index [type] with [field name]" means that you are incorrectly using something like an object, and you are trying to access some field on it.
In your case, this error is pointing at the line : script.Parent.Activate:Connect.This says that script.Parent.Activate is a function and not a table, and you cannot call Connect on a function. This is just to help us figure out what is wrong with our code.
Since you working with Tools, there is a simple fix. Instead of using the Activate function, you are looking for the Activated event. So just change update that line like this :
script.Parent.Activated:Connect(function()
It connects a function to an event that happened, like this:
local tool = script.Parent
-- It connects the event (the tool being equipped) to a function.
tool.Equipped:Connect(function()
-- CODE
end)
Also, the answer above is how you can fix your problem and here's a shortcut: You have defined "Player" and you could've used it inside the function. Have a great day and God bless everyone here.
Heyo. I'm pretty new to Lua (although I do code with Java), so I don't really know anything on this. I'm basically trying to get a user's input, and if it's not the right type, then restart. Now, I'm not sure if it's just Lua or my IDE (I'm using ZeroBrane Studio if that helps), but it won't reinput for whatever reason. (it just loops, meaning it skips the io.read line)
::restart::
...
a = io.read("*number")
if unit == nil then
print("Error! Incorrect Input!\nRestarting...")
goto restart
end
Oh, and yes, I'm using goto commands for restart. I thought that might be what's causing the issue, but I also tried this:
a = io.read("*number") --input non-number
print(a) --prints
a = io.read("*number") --skips
print(a) --prints
When you input a number, it doesn't skip.
Any help would be nice. Thanks in advance.
nvm i solved it myself
local a
repeat
a = io.read(); a = tonumber(a)
if not a then
print("Incorrect Input!\n(Try using only numbers)")
end
until a
::restart::
local a = io.read("*n", "*l")
if a == nil then
io.read("*l") -- skip the erroneous input line
print("Error! Incorrect Input!\nRestarting...")
goto restart
end
P.S.
Feel free to use goto whenever it makes your code more understandable.
For example, using while of repeat-until loop in this code wouldn't make it better (you would need either additional local variable or break statement).
Instead of using the built-in filter of io.read() (which I do think is bugged sometimes) you should consider using an own little function to ensure that the correct data will be give by the user.
This is such a function:
function --[[ any ]] GetUserInput(--[[ string ]] expectedType, --[[ string ]] errorText)
local --[[ bool ]] needInput = true
local --[[ any ]] input = nil
while needInput do
input = GetData()
if ( type(input) == expectedType ) then
needInput = false
else
print(errorText)
end
end
return input
end
You can then call it with:
local userInput = GetUserInput("number", "Error: Incorrect Input! Please give a number.")
Oh and on a sidenote: Goto is considered bad practice.
Ok so I've been searching for a while and didn't get the answer. I imagine someone has this same problem but I was not able to solve this problem. I'm new to Lua, having some experience with Python but not being a programmer :S.
So I'm doing a metatable to handle complex numbers, following the tutorials here: http://www.dcc.ufrj.br/~fabiom/lua/
so I implement creation, addition, printing and equal comparison:
local mt={}
local function new(r,i)
return setmetatable({real = r or 0, im = i or 0},mt)
end
local function is_complex (v)
return getmetatable(v)==mt
end
local function add (c1,c2)
if not is_complex(c1) then
return new(c1+c2.real,c2.im)
end
if not is_complex(c2) then
return new(c1.real+c2,c1.im)
end
return new(c1.real + c2.real,c1.im + c2.im)
end
local function eq(c1,c2)
return (c1.real==c2.real) and (c1.im==c2.im)
end
local function modulus(c)
return (math.sqrt(c.real^2 + c.im^2))
end
local function tos(c)
return tostring(c.real).."+"..tostring(c.im).."i"
end
mt.new=new
mt.__add=add
mt.__tostring=tos
mt.__eq=eq
mt.__len=modulus
return mt
Then I make a small tests:
complex2=require "complex2"
print (complex2)
c1=complex2.new(3,2)
c2=complex2.new(3,4)
print (c1)
print (c2)
print(#{1,2})
print(#c2)
print(complex2.__len(c2))
print(#complex2.new(4,3))
and I get:
table: 0000000003EADBC0
3+2i
3+4i
2
0
5
0
So, What am I dong wrong? is something with calling the #, when i try to debug in the other cases the program goes to the module into the function but the # operand gets like ignored. Modulus function is working and can be called in the module... I'm sorry for such a long and I'm sure obvious question but I tried everything I could already. Thank you
May be the problem is about Lua version.
http://www.lua.org/manual/5.2/manual.html#2.4
"len": the # operation
works since Lua 5.2
and it works only with tables
So, Thank you, and you are right. I thought I had selected 5.2 but in the interpreter it was running 5.1 :S. Now i did:
complex2=require "complex2"
print (complex2)
c1=complex2.new(3,2)
c2=complex2.new(3,4)
print (c1)
print (c2)
print (c1+c2)
print(#{1,2})
print(#c2)
print(complex2.__len(c2))
print(complex2.__len(complex2.new(3,3)))
print(#complex2.new(4,3))
print(getmetatable(c2))
And i got:
table: 000000000047E1C0
3+2i
3+4i
6+6i
2
5
5
4.2426406871193
5
table: 000000000047E1C0
Everything under control ^^, at least the code was working as supposed xD
I've made a small script that makes ragdolls fly upwards. It works but it leaves an error message and I cant figure out why.
[ERROR] RunString:11: Tried to use a NULL physics object!
1. ApplyForceCenter - [C]:-1
2. fn - RunString:11
3. unknown - addons/ulib/lua/ulib/shared/hook.lua:179
The error is getting spammed in console until I delete all existing ragdolls
My code:
hook.Add("Think", "Fly", function()
ent = ents:GetAll()
for k, v in pairs(ent) do
local isRagdoll = v:IsRagdoll()
if isRagdoll == true then
phys = v:GetPhysicsObject()
phys:ApplyForceCenter(Vector(0, 0, 900))
end
end
end)
Thanks in advance.
Henrik's answer is spot on about logic. You do need to make sure the physics object is valid before trying to use it.
In GMod, the function for this is IsValid.
if IsValid(phys) then
I'd have added this as a comment to Henrik's answer but I don't quite have enough rep yet.
Edit: Thanks to MattJearnes for clarifying how to check gmod objects for NULL.
Without knowing anything about gmod's API, I'd guess that GetPhysicsObject can return a special value that depicts NULL, in which case you cannot call ApplyForceCenter on it. You should simply check for NULL before doing anything using IsValid:
hook.Add("Think", "Fly", function()
ent = ents:GetAll()
for k, v in pairs(ent) do
local isRagdoll = v:IsRagdoll()
if isRagdoll == true then
local phys = v:GetPhysicsObject()
if IsValid(phys) then
phys:ApplyForceCenter(Vector(0, 0, 900))
end
end
end
end)