attempt to call global 'getDirectionTo' <a nil value> stack traceback - lua

What is wrong here?
attempt to call global 'getDirectionTo' <a nil value> stack traceback: gateofexp_closed.lua:43: in gateofexp_closed.lua:5
gateofexp_closed.lua
function onUse(cid, item, frompos, item2, topos)
local isLevelDoor = (item.actionid >= 1001 and item.actionid <= 1999)
local isVocationDoor = (item.actionid >= 2001 and item.actionid <= 2008)
if not(isLevelDoor or isVocationDoor) then
-- Make it a normal door
doTransformItem(item.uid, item.itemid+1)
return true
end
local canEnter = true
if(isLevelDoor and getPlayerLevel(cid) < (item.actionid-1000)) then
canEnter = false
end
if(isVocationDoor) then
local doorVoc = item.actionid-2000
if (doorVoc == 1 and not(isSorcerer(cid))) or
(doorVoc == 2 and not(isDruid(cid))) or
(doorVoc == 3 and not(isPaladin(cid))) or
(doorVoc == 4 and not(isKnight(cid))) or
(doorVoc ~= getPlayerVocation(cid)) then
canEnter = false
end
end
if(not canEnter and getPlayerAccess(cid) == 0) then
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Only the worthy may pass.")
return true
end
doTransformItem(item.uid, item.itemid+1)
local canGo = (queryTileAddThing(cid, frompos, bit.bor(2, 4)) == RETURNVALUE_NOERROR) --Veryfies if the player can go, ignoring blocking things
if not(canGo) then
return false
end
local dir = getDirectionTo(getPlayerPosition(cid), frompos)
doMoveCreature(cid, dir)
return true
end

getDirectionTo is not defined when that code is run. This is what the message means. I don't know why this happens, of course.

Related

Lua function constantly returning false out of SQL function

I have this code
function IsAuthorized(xPlayer, doorID, locked, usedLockpick)
local jobName, grade = {}, {}
jobName[1] = xPlayer.job.name
grade[1] = xPlayer.job.grade
if xPlayer.job2 then
jobName[2] = xPlayer.job2.name
grade[2] = xPlayer.job2.grade
end
local canOpen = false
if doorID.lockpick and usedLockpick then
count = xPlayer.getInventoryItem('lockpick').count
if count and count >= 1 then canOpen = true end
end
if not canOpen and doorID.authorizedJobs then
for job,rank in pairs(doorID.authorizedJobs) do
if (job == jobName[1] and rank <= grade[1]) or (jobName[2] and job == jobName[2] and rank <= grade[2]) then
canOpen = true
if canOpen then break end
end
end
end
if not canOpen and doorID.items then
local count
for k,v in pairs(doorID.items) do
count = xPlayer.getInventoryItem(v).count
if count and count >= 1 then
canOpen = true
local consumables = { ['ticket']=1 }
if locked and consumables[v] then
xPlayer.removeInventoryItem(v, 1)
end
break
end
end
if not count or count < 1 then canOpen = false end
end
if not canOpen then
local group = xPlayer.getGroup()
if group == 'management' or group == 'owner' then
print(group..' '..xPlayer.getName()..' was authorised to use a door')
canOpen = true
end
if doorID.authorizedgang then
print(xPlayer.identifier .. " | " .. doorID.authorizedgang)
MySQL.Async.fetchSingle('SELECT * FROM users WHERE identifier = ? AND gang = ?', {xPlayer.identifier, doorID.authorizedgang}, function(foundplayer)
if foundplayer then
print("found")
canOpen = true
print(canOpen)
end
----------------------------canopen returns true until here
end)
end
end
print(canOpen)
return canOpen
end
Essentially what is does is return a true or false value based on the if statements. I did not write this script myself, I simply added on. The code that I added to this function is
if doorID.authorizedgang then
print(xPlayer.identifier .. " | " .. doorID.authorizedgang)
MySQL.Async.fetchSingle('SELECT * FROM users WHERE identifier = ? AND gang = ?', {xPlayer.identifier, doorID.authorizedgang}, function(foundplayer)
if foundplayer then
print("found")
canOpen = true
print(canOpen)
end
----------------------------canopen returns true until here
end)
end
This code essentially makes a database call to check if it'll find a player with X identifier and Y gang, and this actually does work because it prints out "found" and canOpen returns true until the line that I marked, after this it returns false at the bottom of the function.
I've been trying other ways to get the data but ultimately I need the SQL DB call for this and im not sure why the variable isnt setting when in fact the database returns found. Any ideas what I could be doing wrong here?

how do i fix ')' (to close '(' at line 38), got '.'

for i, v in pairs(Buttons:GetChildren()) do
local NewItem = BoughtItems:FindFirstChild(v.Item.value)
if NewItem ~= nil then
Items[NewItem.Name] = NewItem:Clone()
NewItem:Destroy()
else
v.ButtonPart.Transparency = 1
v.ButtonPart.CanCollide = false
v.ButtonPart.BillBoardGui.Frame.Visible = false
end
if v:FindFirstChild("Dependency") then
coroutine.resume(coroutine.create(function(
v.ButtonPart.Transparency = 1
v.ButtonPart.CanCollide = false
v.ButtonPart.BillBoardGui.Frame.Visible = false
if BoughtItems:WaitForChild(v.Dependency.Value, 100000)then
v.ButtonPart.Transparency = 0
v.ButtonPart.CanCollide = true
v.ButtonPart.BillBoardGui.Frame.Visible = true
end
end))
end
v.ButtonPart.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid")then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Values.OwnerValue.Value == Player then
if v.ButtonPart.CanCollide == true and v.ButtonPart.Transparency == 0 then
if Player:WaitForChild("leaderstats").Cash.Value >= v.Price.Value then
Player.leaderstats.Cash.Value -= v.Price.Value
Items[v.Item.Value].Parent = BoughtItems
v:Destroy()
end
end
end
end
end)
end
how do i fix this the header is the problem i dont knw what it means so is there a fix to it for my tycoon if anyone knows pls comment (heres some random stuff since my post is mostly code) A brief Introduction to Copypasta. Copypasta means to copy a text or a part of the text from an existing manuscript and inclusion of that very text in an under-process manuscript by pasting. At present, the societies are going to be expanded with an enormous pattern.
Parenthesis always come in pairs. The error message already strongly suggests that you are missing a closing parenthesis for an opening one in line 38.
1 2 3
coroutine.resume(coroutine.create(function(
v.ButtonPart.Transparency = 1
v.ButtonPart.CanCollide = false
v.ButtonPart.BillBoardGui.Frame.Visible = false
if BoughtItems:WaitForChild(v.Dependency.Value, 100000)then
v.ButtonPart.Transparency = 0
v.ButtonPart.CanCollide = true
v.ButtonPart.BillBoardGui.Frame.Visible = true
end
end)) <--- one is missing
1 2
Use a text editor that autocompletes pairs or even better one that hightlights pairs in matching colors to avoid errors like this.

Roblox Attempt to index nil with 'WaitForChild'

I'm making a script that is supposed to find the targeted player, if a value inside the player is true, then MoveTo(). It outputs an error that says
Attempt to index nil with 'WaitForChild'
This is a server script and the error is at plr:WaitForChild("Crime",1).
Here it is:
local crime= false
local plr = nil
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 10000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
plr = game.Players:WaitForChild(temp2.Name,1)
print("plr")
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while true do
wait(1)
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil and plr:WaitForChild("Crime",1).Value==true then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end
You can not guarantee that plr is set after findNearestTorso runs.
You set plr in the body of 3 nested if statements.
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
--...
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
plr = game.Players:WaitForChild(temp2.Name,1)
--...
if any of your if statements are not true plr will be nil
To handle that we can simply change your condition, where the error happens, to check if plr is nil before the attempt to call plr:WaitForChild
if target ~= nil and plr ~= nil and plr:WaitForChild("Crime",1).Value==true then

Trying to index a Parent results in a nil value

Error:
16:16:03.496 - Workspace.Storeroom.Pile of Crates.Wood Crate.Script:17: attempt to index field 'Parent' (a nil value)
Script:
local db = true
local clickdetector = script.Parent:WaitForChild('ClickDetector')
clickdetector.MouseClick:Connect(function(plr)
local randomizer = math.random(1,6)
if randomizer == 1 or 2 or 3 then
script.Parent:Destroy()
local gift = game:GetService("ReplicatedStorage"):WaitForChild("Blue Keycard")
if db == true then
db = false
gift:Clone().Parent = plr.Backpack
wait(1)
db = true
end
end
if randomizer == 4 or 5 then
script.Parent:Destroy()
local gift = game:GetService("ReplicatedStorage"):WaitForChild("Red Keycard")
if db == true then
db = false
gift:Clone().Parent = plr.Backpack
wait(1)
db = true
end
end
if randomizer == 6 then
script.Parent:Destroy()
local gift = game:GetService("ReplicatedStorage"):WaitForChild("Green Keycard")
if db == true then
db = false
gift:Clone().Parent = plr.Backpack
wait(1)
db = true
end
end
end)
if randomizer == 1 or 2 or 3 then
if randomizer == 4 or 3 then
These lines won't work as you think it should. It doesn't check if randomizer value is in some set of predefined values. Expression randomizer == 1 or 2 or 3 evaluates (depending on randomizer exact value) either to true or to 2, which is interpreted as true in if/then construct.
If you really want to check the value against the list, you should do something like:
local set1 = {1=true, 2=true, 3=true}
local set2 = {4=true, 5=true}
-- somewhere much later
if set1[randomizer] then .. end
Start with fixing this issue.

lua variable name from user input

I am trying to make a "shell" in lua.
But the main problem is, I can not define the variable name from user input.
Here is the core of what I currently have. I am having an issue with the what[2] = what[3] line with the comment below.
How can I better implement this?
function lsplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
function def(what)
if (what[1] == "end") then
os.exit(0)
elseif (what[1] == "help") then
print("Commander version 0.0")
elseif (what[1] == "var") then
what[2] = what[3] --Can not define
else
print("[ERR] not a command!")
end
end
while(true) do
io.write("-->")
local usr = io.read("*l")
local cmd = lsplit(usr, " ")
def(cmd)
end
you are overwriting you first parameter with you second one, and not creating a new var... try this code! Should work but it is untested!
local userdefinedVars = { }
function lsplit(inputstr)
words = {}
for word in s:gmatch("%w+") do
table.insert(words, word)
end
end
function def(what)
if (what[1] == "end") then
os.exit(0)
elseif (what[1] == "help") then
print("Commander version 0.0")
elseif (what[1] == "var") then
-- This is how you get your things done!
userdefinedVars[what[2]] = what[3]
else
print("[ERR] not a command!")
end
end
while(true) do
io.write("--> ")
local usr = io.read("*line")
local cmd = lsplit(usr)
def(cmd)
end

Resources