local sideIn = "front"
rednet.open("back")
while true do
if disk.isPresent(sideIn) then
term.clear()
local code = fs.open("disk/passcode.lua", "r").readAll()
if code == nil then
local code = 0
else
code = tonumber(code)
end
if code == level then
disk.eject(sideIn)
rednet.send(16,"1")
else
disk.eject(sideIn)
end
else
os.queueEvent("randomEvent")
os.pullEvent()
end
end
If i run the program with a disk in, it works.
But if i run it THEN insert the disk I get an error:
startup:11: attempt to index a nil value
if someone can help, i would apreciate that
Related
I am learning about gui and statemachine with love2d. I made a simple app which have menu, run and ended state. However I got an problem in the ended state with the buttons.
Here is the update and exit part of ended.lua:
function m:update(dt, _stateMachine)
for i = 1, #self.buttons do
self.buttons[i]:detect(dt) ------ Attempt to index a nil value
if self.buttons[i].waitAnimEnded == true then
if i == 1 then
_stateMachine:change(gstates.run)
elseif i == 2 then
_stateMachine:change(gstates.menu)
end
end
end
function m:exit()
if gstates.run.score > gstates.run.maxScore then
gstates.run.maxScore = gstates.run.score
end
self.buttons = {}
end
However, I got no error with the run.lua, I wrote it with the same way as the ended.lua.
run.lua:
function m:update(dt, _stateMachine)
for i = 1, #self.buttons do
self.buttons[i]:detect(dt)
if self.buttons[i].waitAnimEnded == true then
if i == 1 then
self.score = self.score + 1
self.buttons[i].waitAnimEnded = false
elseif i == 2 then
_stateMachine:change(gstates.ended)
end
end
end
end
function m:exit()
self.buttons = {}
end
And here is the stateMachine.lua
sm = {}
sm.currentState = gstates.menu
function sm:change(_newState)
self.lastState = self.currentState
self.currentState = _newState
self.lastState:exit()
self.currentState:enter()
end
function sm:update(dt)
self.currentState:update(dt, self)
end
function sm:render()
self.currentState:render()
end
return sm
After I did a little changed in ended.lua, the error fixed, but idk why would that happened.
function m:update(dt, _stateMachine)
for i = 1, #self.buttons do
if not(#self.buttons == 0) then ---- I changed here -----
self.buttons[i]:detect(dt)
if self.buttons[i].waitAnimEnded == true then
if i == 1 then
_stateMachine:change(gstates.run)
elseif i == 2 then
_stateMachine:change(gstates.menu)
end
end
end
end
end
If you have an idea of this problem, please tell me. Thank you.
Let's assume self.buttons contains two values.
for i = 1, #self.buttons do
print(self.buttons[i])
_stateMachine:change(gstates.ended)
end
This will print a button, and nil. And this nil causes the attempt to index a nil value. Why? Because in _stateMachine:change you call self.lastState:exit(), and in exit you remove all buttons via self.buttons = { } and then the second button no longer exists.
If you change the state, break the button loop.
I tried to fix some Garry's Mod addon and this is what happens. I tried to fix it for long time, but I'm not the best in Lua coding :/ . What is wrong with this code? I get this error:
[ERROR] addons/garrys_bombs_5_base_528449144/lua/entities/gb5_shockwave_sound_lowsh.lua:80: bad argument #1 to 'SetPhysicsAttacker' (Entity expected, got nil)
1. SetPhysicsAttacker - [C]:-1
2. unknown - addons/garrys_bombs_5_base_528449144/lua/entities/gb5_shockwave_sound_lowsh.lua:80
And the code is pretty long. I have every file working fine, but this file is not working
AddCSLuaFile()
DEFINE_BASECLASS( "base_anim" )
if (SERVER) then
util.AddNetworkString( "gb5_net_sound_lowsh" )
end
ENT.Spawnable = false
ENT.AdminSpawnable = false
ENT.PrintName = ""
ENT.Author = ""
ENT.Contact = ""
ENT.GBOWNER = nil
ENT.MAX_RANGE = 0
ENT.SHOCKWAVE_INCREMENT = 0
ENT.DELAY = 0
ENT.SOUND = ""
net.Receive( "gb5_net_sound_lowsh", function( len, pl )
local sound = net.ReadString()
LocalPlayer():EmitSound(sound)
end );
function ENT:Initialize()
if (SERVER) then
self.FILTER = {}
self:SetModel("models/props_junk/watermelon01_chunk02c.mdl")
self:SetSolid( SOLID_NONE )
self:SetMoveType( MOVETYPE_NONE )
self:SetUseType( ONOFF_USE )
self.Bursts = 0
self.CURRENTRANGE = 0
self.GBOWNER = self:GetVar("GBOWNER")
self.SOUND = self:GetVar("SOUND")
end
end
function ENT:Think()
if (SERVER) then
if not self:IsValid() then return end
local pos = self:GetPos()
self.CURRENTRANGE = self.CURRENTRANGE+(self.SHOCKWAVE_INCREMENT*10)
if(GetConVar("gb5_realistic_sound"):GetInt() >= 1) then
for k, v in pairs(ents.FindInSphere(pos,self.CURRENTRANGE)) do
if v:IsPlayer() then
if not (table.HasValue(self.FILTER,v)) then
net.Start("gb5_net_sound_lowsh")
net.WriteString(self.SOUND)
net.Send(v)
v:SetNWString("sound", self.SOUND)
if self:GetVar("Shocktime") == nil then
self.shocktime = 1
else
self.shocktime = self:GetVar("Shocktime")
end
if GetConVar("gb5_sound_shake"):GetInt()== 1 then
util.ScreenShake( v:GetPos(), 5555, 555, self.shocktime, 500 )
end
table.insert(self.FILTER, v)
end
end
end
else
if self:GetVar("Shocktime") == nil then
self.shocktime = 1
else
self.shocktime = self:GetVar("Shocktime")
end
local ent = ents.Create("gb5_shockwave_sound_instant")
ent:SetPos( pos )
ent:Spawn()
ent:Activate()
ent:SetPhysicsAttacker(ply)
ent:SetVar("GBOWNER", self.GBOWNER)
ent:SetVar("MAX_RANGE",50000)
ent:SetVar("DELAY",0.01)
ent:SetVar("Shocktime",self.shocktime)
ent:SetVar("SOUND", self:GetVar("SOUND"))
self:Remove()
end
self.Bursts = self.Bursts + 1
if (self.CURRENTRANGE >= self.MAX_RANGE) then
self:Remove()
end
self:NextThink(CurTime() + (self.DELAY*10))
return true
end
end
function ENT:OnRemove()
if SERVER then
if self.FILTER==nil then return end
for k, v in pairs(self.FILTER) do
if not v:IsValid() then return end
v:SetNWBool("waiting", true)
end
end
end
function ENT:Draw()
return false
end
Is there a chance someone fix this for me? Or even just telling me what's wrong? I would be pleased. If needed I can send all files. Well... It's not my addon but I'm trying to fix an existing one. Someone tried to fix it too but he didn't (actually he broke it even more).
What the error means
Inside your ENT:Think() function, you are calling ent:SetPhysicsAttacker(ply)
ply is not defined anywhere inside that function, so is nil (Entity expected, got nil)
How to fix this
If no player is responsible for the damage caused by this entity, delete the line ent:SetPhysicsAttacker(ply).
Otherwise, assign an Owner to the entity at the point of creation, using SetOwner.
This would then allow you to use self:GetOwner() inside your Think hook
Example
hook.Add("PlayerSay", "SpawnEntity", function(ply, text)
if string.lower(text) == "!spawnentity" then
-- Create your entity
local myEntity = ents.Create("gb5_shockwave_sound_lowsh")
myEntity:SetPos(ply:GetPos())
myEntity:SetAngles(ply:GetAngles())
myEntity:Spawn()
-- Sets the owner to the player that typed the command
myEntity:SetOwner(ply)
return ""
end
end)
-- Inside your entity code
function ENT:Think()
print("My owner is: " .. tostring(self:GetOwner()))
-- ...
ent:SetPhysicsAttacker(self:GetOwner())
end
I am having an error with one of my scripts in Roblox Studio. The line that shows that is a problem is Line 13. The script is for basically to weld when somebody sits on the seat, and to start up the TankGUI (Another script that is fine).
seat = script.Parent
function onChildAdded(part)
if (part.className == "Weld") then
while true do
local welde = seat:FindFirstChild("SeatWeld")
if (welde ~= nil) then
local sitted = welde.Part1.Parent
end
if (sitted.Name ~= script.Parent.Owner.Value) then
if (script.Parent.Owner.Value == "NoOne") then
script.Parent.Owner.Value = sitted.Name
print ("sitted steal")
else
print ("stolen!!")
local shipstealer = game.Workspace:FindFirstChild(sitted.Name)
if (shipstealer ~= nil) then
shipstealer.Humanoid.Jump=true
end
end
end
wait(0.2)
if (welde == nil) then
print("BreakLoop")
script.Parent.Owner.Value = "NoOne"
break end
end
end
end
--while true do
-- wait(0.5)
-- script.Parent.Parent.Name=script.Parent.Owner.Value .. "'s Ship"
--end
seat.ChildAdded:connect(onChildAdded)
Please excuse any poor co-operation or language barriers I'm having. I'm still a bit new here.
if (welde ~= nil) then
local sitted = welde.Part1.Parent
end
if (sitted.Name ~= script.Parent.Owner.Value) then
Since you're declaring sitted as local, it goes out of scope at end, so it's gone when you try to use it in that if, so you're actually using a nil global variable there. Do this instead:
local sitted
if (welde ~= nil) then
sitted = welde.Part1.Parent
end
if (sitted.Name ~= script.Parent.Owner.Value) then
That way it'll stay in scope until you're actually done with it.
local level = 3 -- Required access level
local sideIn = "bottom" -- Keycard Input Side
local sideOut = "right" -- Redstone output side
local rsTime = 3 -- Redstone time
while true do
if disk.isPresent(sideIn) then
term.clear()
term.setCursorPos(1,1)
local code = fs.open("disk/passcode.lua", "r").readAll()
if code == nil then
local code = 0
else
local code = tonumber(code)
end
if code >= level then
print("> Access Granted")
disk.eject(sideIn)
rs.setOutput(sideOut,true)
sleep(rsTime)
rs.setOutput(sideOut,false)
else
print("> Permission Denied")
disk.eject(sideIn)
end
end
end
When there's no disk inserted, it throws an error:
.temp:15: attempt to compare string with number expected, got string
Does anyone know how to fix this issue? I throwed in a nil checker but it seems to not work. Any ideas on how could I fix this? I've been trying for at least half an hour now, and I still have no clue.
In this section:
local code = fs.open("disk/passcode.lua", "r").readAll() --(1)
if code == nil then
local code = 0 --(2)
else
local code = tonumber(code) --(3)
end
It first makes a new local variable with local code = .... In the new block that you create with the if, you also create new local variables with local code = .... Since it has the same name as the local before it, it "masks" it, prohibiting you from accessing the first code in the rest of the block. The value that you assign 0 to is not the same variable outside the if, so the first code is unaffected. At else the block for the second code ends and the same thing happens between else and end when the condition is false. To not assign the values 0 or tonumber(code) to new variables, you have to remove the local from local code = .... So the following is what it should be:
local level = 3 -- Required access level
local sideIn = "bottom" -- Keycard Input Side
local sideOut = "right" -- Redstone output side
local rsTime = 3 -- Redstone time
while true do
if disk.isPresent(sideIn) then
term.clear()
term.setCursorPos(1,1)
local code = fs.open("disk/passcode.lua", "r").readAll()
if code == nil then
code = 0
else
code = tonumber(code)
end
if code >= level then
print("> Access Granted")
disk.eject(sideIn)
rs.setOutput(sideOut,true)
sleep(rsTime)
rs.setOutput(sideOut,false)
else
print("> Permission Denied")
disk.eject(sideIn)
end
end
end
I am having this error, can't find the error.
local choice_revive = {function(player,choice)
local user_id = vRP.getUserId(player)
if user_id ~= nil then
vRPclient.getNearestPlayer(player,{10},function(nplayer)
local nuser_id = vRP.getUserId(nplayer)
if nuser_id ~= nil then
vRPclient.isInComa(nplayer,{}, function(in_coma)
if in_coma then
if vRP.tryGetInventoryItem(user_id,"medkit",1,true) else
vRP.tryGetInventoryItem(user_id,"smartwatch",1,true)
io.write("Smartwatch: Tilkalder Ambulance")
then
vRPclient.playAnim(player,{false,revive_seq,false}) -- anim
SetTimeout(15000, function()
vRPclient.varyHealth(nplayer,{50}) -- heal 50
end)
end
else
vRPclient.notify(player,{lang.emergency.menu.revive.not_in_coma()})
end
end)
else
vRPclient.notify(player,{lang.common.no_player_near()})
end
end)
end
end,lang.emergency.menu.revive.description()}
I have tried to make, and look for error, but without luck.
- If anyone can fix it, tell me please.
if statement, functions and for statement must be ended with end.
this code must be like that
--Settings--
local Tunnel = module("vrp", "lib/Tunnel")
local Proxy = module("vrp", "lib/Proxy")
vRP = Proxy.getInterface("vRP")
vRPclient = Tunnel.getInterface("vRP","vRP_smartwatch")
if in_coma then
vRP.tryGetInventoryItem(user_id,"smartwatch",1,true)
io.write("Smartwatch: Tilkalder Ambulance")
end
function vRP.sendServiceAlert(sender, emergency,x,y,z, msg)
local service = services[service_name]
local answered = false
if service then
local players = {}
for k,v in pairs(vRP.rusers) do
local player = vRP.getUserSource(tonumber(k))
-- check user
if vRP.hasPermission(k,service.alert_permission) and player ~= nil then
table.insert(players,player)
end
end
end
end