Can anybody see what is wrong with this Roblox script? - lua

Can anybody see what is wrong with this Roblox script?
local a1 = game.CoreGui.DBXBRGUI.Menu
local a2 = game.CoreGui.DBXBRGUI.Opener
if a1.Visible == true then do
a2.Visible = false
elseif a2.Visible == true then do
a1.Visible = false
end

First off, don't put "do" after "then" (that's your problem)
Second, just use else not elseif because something may be different.
This is what it should look like:
local a1 = game.CoreGui.DBXBRGUI.Menu
local a2 = game.CoreGui.DBXBRGUI.Opener
if a1.Visible == true then
a2.Visible = false else
a1.Visible = false
end
I'm using common sense to say that this is probably what you want:
local a1 = game.CoreGui.DBXBRGUI.Menu
local a2 = game.CoreGui.DBXBRGUI.Opener
if a1.Visible == true then
a1.Visible = true
a2.Visible = false else
a1.Visible = false
a2.Visible = true
end

First off you are trying to reference CoreGui which is strictly for Roblox's default GUIs. Any custom GUIs you make will be in each Player's PlayerGui. That is referenced by doing game.Players.LocalPlayer.PlayerGui.
It is worth noting that for anything clientside (such as GUIs), you should check if the objects exist before trying to use them. This is done via the parent:FindFirstChild(name) or parent:WaitForChild(name) functions.
Secondly, you are using a "do-end"-block inside your conditional statement. A "do-end" block requires an end. You also do not need that block, so I suggest removing it. If we add the end to the correct location in your code, we get this:
if CONDITION then
do
-- stuff
end -- you're missing this end
elseif CONDITION2 then
do
end -- you're also missing this end
end
Because of the missing ends, your code is interpreted as follows:
if CONDITION then
do
elseif CONDITION2 then
do
end
As you see, the "elseif" appears inside the do-block inside the first if, instead of as an elseif to the first if. You cannot have an "elseif"-statement without an if, so that's what errors.
Your code would ideally look like this:
local Gui = game.Players.LocalPlayer.PlayerGui:WaitForChild("DBXRGUI")
local a1 = Gui.Opener
local a2 = Gui.Menu
if a1 == true then
a1.Visible = false
elseif a2.Visible == true then
a2.Visible = false
end

Related

Functions are having a problem where the 'end' part is not working

For some reason, I think the error messages are telling me to both take away a bracket or add one and I'm very confused. It is always either telling me that there is supposed to be something instead of the bracket or that a bracket is needed depending on how I change it.
Here's the code:
local Ammo = MaxAmmo
local Reloading = false
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Icon = "rbxassetid://1008161057"
local function Reload()
Reloading = true
Mouse.Icon = "rbxassetid://1008161057"
wait(2)
Ammo = MaxAmmo
Reloading = false
Mouse.Icon = "rbxassetid://1008161057"
end
script.Parent.Activated:Connect(function()
if Ammo>0 and not Reloading then
Ammo=Ammo-1
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
if Mouse.Target == ("Head") then
print('head')
Mouse.Target.Parent.Humanoid:TakeDamage(40)
else do
Mouse.Target.Parent.Humanoid:TakeDamage(20)
print('body')
end
end
elseif Reloading == false then
Reload()
end
print(Ammo)
end)
--local Input = game:GetService("UserInputService")
--Input.InputBegan:Connect(function(Key)
--if Key.Keycode == Enum.KeyCode.r and Reloading == false and Ammo~=MaxAmmo then
--Reload()
--end
--end)
end)```
if Mouse.Target == ("Head") then
print('head')
Mouse.Target.Parent.Humanoid:TakeDamage(40)
else do
Mouse.Target.Parent.Humanoid:TakeDamage(20)
print('body')
end
Either add an end to that do or remove it.
To fix such errors count all keywords that require an end and all ends and see if you get the same number. There are text editors that help you with that
You should use an IDE like ZeroBraneStudio, or IntelliJ Community (with EmmyLua)
First issue:
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
if Mouse.Target == ("Head") then
print('head')
Mouse.Target.Parent.Humanoid:TakeDamage(40)
else do
Mouse.Target.Parent.Humanoid:TakeDamage(20)
print('body')
end
end
You should remove do from else do
Second issue:
You'll be getting "unexpected )" from here:
print(Ammo)
end)
This is because you're missing an end from if Ammo > 0 and not Reloading then
The resultant code should look like this:
local Ammo = MaxAmmo
local Reloading = false
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Icon = "rbxassetid://1008161057"
local function Reload()
Reloading = true
Mouse.Icon = "rbxassetid://1008161057"
wait(2)
Ammo = MaxAmmo
Reloading = false
Mouse.Icon = "rbxassetid://1008161057"
end
script.Parent.Activated:Connect(function()
if Ammo > 0 and not Reloading then
Ammo = Ammo - 1
if Mouse.Target.Parent:FindFirstChild("Humanoid") then
if Mouse.Target == ("Head") then
print('head')
Mouse.Target.Parent.Humanoid:TakeDamage(40)
else
Mouse.Target.Parent.Humanoid:TakeDamage(20)
print('body')
end
elseif Reloading == false then
Reload()
end
print(Ammo)
end
end)
--local Input = game:GetService("UserInputService")
--Input.InputBegan:Connect(function(Key)
--if Key.Keycode == Enum.KeyCode.r and Reloading == false and Ammo~=MaxAmmo then
--Reload()
--end
--end)
end)

<name> error while trying to do two or more loops

Last time I wanted to update my LUA code to be more automatic. Earlier had to turn on and off everything manual, by deleting phrases. But I met small problem name expected near '2' error and I'm not sure what's wrong is with it. I was looking in other thread about similar error, but they didn't help me.
local BOSS = GetModConfigData("BOSS") --return true or false
local KOAL = GetModConfigData("KOAL") --return true or false
local SCULP = GetModConfigData("SCULP") --return true or false
local BossList = {"BOSS", "KOAL", "SCULP"}
local BossCode = {"bosscode", "koal_old", "koal_new", "sculp_small", "sculp_med", "sculp_big"}
k = 1
for i,v in ipairs(BossList) do
if BossList[i] == true then
if BossList[i] == "KOAL" then
for i,2 do
Bosses[k] = BossCode[k]
k = k+1
end
elseif BossList[i] == "SCULP" then
for i,3 do
Bosses[k] = BossCode[k]
k = k+1
end
else
Bosses[k] = BossCode[k]
k = k+1
end
end
end
When I'm trying to use second loop, problem comes to me. When I pushed this code without second and third loop, it was working. But without save additional BossCodes.
for i,v in ipairs(BossList) do
if BossList[i] == true then
Bosses[k] = BossCode[k]
k = k+1
end
end

im having troubles getting my toggle script working in roblox

i need help
this is the script:
script.Parent.Parent.Activated:connect(function()
local a = game.Workspace.LogRideToggled.Value
if a == true then
a = false
script.Parent.Click:Play()
end
if a == false then
a = true
script.Parent.Click:Play()
end
end)
this is the hirachy:
https://imgur.com/a/4FXHY
but NOTHING happens, no errors either, except for the click sound playing
i seriously need help
The problem is is that after you do a == true, you set a to false, which a == false then matches after.
You can solve this with an if then else end statement, like so:
script.Parent.Parent.Activated:connect(function()
local a = game.Workspace.LogRideToggled.Value
if a == true then
a = false
script.Parent.Click:Play()
else
a = true
script.Parent.Click:Play()
end
end)
However, this will only change the local value of a, which means that it will not save the change.
To fix this, we need to assign the game.Workspace.LogRideToggleds value of Value directly, which we can do like:
script.Parent.Parent.Activated:connect(function()
if game.Workspace.LogRideToggled.Value == true then
game.Workspace.LogRideToggled.Value = false
script.Parent.Click:Play()
else
game.Workspace.LogRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Although it's bad practice to repeatedly index this like this, so we can store game.Workspace.LogRideToggled in a local variable. You can read up on why this works but storing value doesn't here
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value == true then
logRideToggled.Value = false
script.Parent.Click:Play()
else
logRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Also, the == true is redundant, as Lua expects a truthy or falsey value as the condition, all == true does in this case is give true or false if it's true of false.
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value then
logRideToggled.Value = false
script.Parent.Click:Play()
else
logRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Yet we can clean this up a bit more, as we use script.Parent.Click:Play() in both cases, and we can replace logRideToggled.Value = with a logical not, like so.
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value then
-- Todo if truthy
else
-- Todo if falsey
end
logRideToggled.Value = not logRideToggled.Value
script.Parent.Click:Play()
end)
But if you only want to toggle this value, not do anything special for either case, we can remove that entire conditional, leaving:
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
logRideToggled.Value = not logRideToggled.Value
script.Parent.Click:Play()
end)
Hope this has helped!

Cloning+Dragging model when button is click ROBLOX [LUA]?

So I thought of having this for so long, I just don't know where to start. I am new to this language and I keep learning, but kind of hard for me. But I have built my very own custom character which took 2 weeks for me. Anyway, For my question. An example is if I have a button and I click it, a model will be clone and I can drag that model and put it anywhere nearby. What possible method I can use to achieve this?
First things first, I suggest for any future questions, you head over to https://scriptinghelpers.org/
now, on to your question, for cloning the model, you should use mouse.Target.Parent:Clone() or the GetTopParent(mouse.Target) function in my function library (which you can get here; http://www.roblox.com/item.aspx?id=244244638)
then deposit the model into workspace and MakeJoints()
the next step is to move the model, this can be tricky, but the simplest method is model:MoveTo(mouse.Hit.p) on mouse.Moved (but that's a little buggy)
Another method for movement would be to use the Handles class, but I'm not really familiar with it, so you'd have to figure that one out on your own.
To make the first method less buggy, I'd suggest something along the lines of
model:MoveTo(mouse.Hit.p.X, mouse.Target.Position.Y + (model:GetExtentsSize().Y / 2), mouse.Hit.p.Z)
but you'd have to set up the mouse to ignore the model, which I can't really help with.
A really good place to start is to search the free models in Studio Toolbox for a 'Dragger Tool' or 'Model Dragger Tool' and then use the script inside to get started creating your own. I have learned to create my own custom draggers by doing this and it is way easier than you may think at first. Once you find a good dragger tool to borrow code from, if you need to enhance it, you can find the dragger api in the Roblox Wiki to help you further customize it to your specific needs.
http://wiki.roblox.com/index.php?title=API:Class/Dragger
EDIT: So here's the first dragger script that showed when I searched. It will drag models and parts but you will have to edit it to meet your requirements using the dragger api. Create a Tool in player.BackPack then create a LocalScript inside the Tool then copy and paste the code below into the LocalScript, and that will get you started.
local Tool = script.Parent
enabled = true
local origTexture = Tool.TextureId
game:GetService("ContentProvider"):Preload("rbxasset://icons/freemove_sel.png")
local selectionBox
local currentSelection
local currentSelectionColors = {}
local selectionLasso
local inGui = false
local inPalette = false
local lockTime = 0
function canSelectObject(part)
return part and not (part.Locked) and (part.Position - script.Parent.Parent.Head.Position).Magnitude < 60
end
function findModel(part)
while part ~= nil do
if part.className == "Model" then
return part
end
part = part.Parent
end
return nil
end
function startDrag(mousePart, hitPoint, collection)
dragger = Instance.new("Dragger")
pcall(function() dragger:MouseDown(mousePart, hitPoint, collection) end)
end
function collectBaseParts(object, collection)
if object:IsA("BasePart") then
collection[#collection+1] = object
end
for index,child in pairs(object:GetChildren()) do
collectBaseParts(child, collection)
end
end
function onMouseDown(mouse)
mouse.Icon ="rbxasset://textures\\GrabRotateCursor.png"
local part = mouse.Target
if canSelectObject(part) then
local hitPoint = mouse.Hit:toObjectSpace(part.CFrame).p
if trySelection(part) then
local instances = {}
collectBaseParts(currentSelection, instances)
startDrag(part, hitPoint, instances)
return
end
end
--Clear the selection if we weren't able to lock succesfullu
onMouseUp(mouse)
end
function onMouseUp(mouse)
mouse.Icon ="rbxasset://textures\\GrabCursor.png"
if dragger ~= nil then
pcall(function() dragger:MouseUp() end)
dragger = nil
end
end
function trySelection(part)
if canSelectObject(part) then
selectionLasso.Part = part
local model = findModel(part)
if model then
return setSelection(model)
else
return setSelection(part)
end
else
clearSelection()
return false
end
end
function onKeyDown(key)
if dragger ~= nil then
if key == 'R' or key == 'r' then
dragger:AxisRotate(Enum.Axis.Y)
elseif key == 'T' or key == 't' then
dragger:AxisRotate(Enum.Axis.Z)
end
end
end
local alreadyMoving
function onMouseMove(mouse)
if alreadyMoving then
return
end
alreadyMoving = true
if dragger ~= nil then
--Maintain the lock
if time() - lockTime > 3 then
Instance.Lock(currentSelection)
lockTime = time()
end
--Then drag
pcall(function() dragger:MouseMove(mouse.UnitRay) end)
else
trySelection(mouse.Target)
end
alreadyMoving = false
end
function saveSelectionColor(instance)
if instance:IsA("BasePart") then
currentSelectionColors[instance] = instance.BrickColor
if instance.BrickColor == BrickColor.Blue() then
instance.BrickColor = BrickColor.new("Deep blue")
else
instance.BrickColor = BrickColor.Blue()
end
end
local children = instance:GetChildren()
if children then
for pos, child in pairs(children) do
saveSelectionColor(child)
end
end
end
function setSelection(partOrModel)
if partOrModel ~= currentSelection then
clearSelection()
if Instance.Lock(partOrModel) then
lockTime = time()
currentSelection = partOrModel
saveSelectionColor(currentSelection)
selectionBox.Adornee = currentSelection
return true
end
else
if currentSelection ~= nil then
if time() - lockTime > 2 then
--Maintain the lock
if not(Instance.Lock(currentSelection)) then
--we lost the lock
clearSelection()
return false
else
lockTime = time()
return true
end
else
return true
end
end
end
return false
end
function clearSelection()
if currentSelection ~= nil then
for part, color in pairs(currentSelectionColors) do
part.BrickColor = color
end
selectionBox.Adornee = nil
Instance.Unlock(currentSelection)
end
currentSelectionColors = {}
currentSelection = nil
selectionLasso.Part = nil
selectionBox.Adornee = nil
end
function onEquippedLocal(mouse)
Tool.TextureId = "rbxasset://icons/freemove_sel.png"
local character = script.Parent.Parent
local player = game.Players:GetPlayerFromCharacter(character)
inGui = false
inPalette = false
mouse.Icon ="rbxasset://textures\\GrabCursor.png"
mouse.Button1Down:connect(function() onMouseDown(mouse) end)
mouse.Button1Up:connect(function() onMouseUp(mouse) end)
mouse.Move:connect(function() onMouseMove(mouse) end)
mouse.KeyDown:connect(function(string) onKeyDown(string) end)
selectionBox = Instance.new("SelectionBox")
selectionBox.Name = "Model Delete Selection"
selectionBox.Color = BrickColor.Blue()
selectionBox.Adornee = nil
selectionBox.Parent = player.PlayerGui
selectionLasso = Instance.new("SelectionPartLasso")
selectionLasso.Name = "Model Drag Lasso"
selectionLasso.Humanoid = character.Humanoid
selectionLasso.archivable = false
selectionLasso.Visible = true
selectionLasso.Parent = game.workspace
selectionLasso.Color = BrickColor.Blue()
alreadyMoving = false
end
function onUnequippedLocal()
Tool.TextureId = origTexture
clearSelection()
selectionBox:Remove()
selectionLasso:Remove()
end
Tool.Equipped:connect(onEquippedLocal)
Tool.Unequipped:connect(onUnequippedLocal)

Attempt to index a nil value in a script

I'm trying to write a lua script to assist in modifying a game, and it keeps breaking on one particular line of one of my assistant libraries.
odfWriter.lua:
require 'loopsetup'
require 'ioWriter'
local open = {}
odfWriter = class{
writer = false
}
odfWriter[open] = false
function odfWriter:open(name)
if not self[open] then
self.writer = ioWriter()
self.writer:open(name)
self[open] = true
else
error("tried to open an already open writer")
end
end
function odfWriter:write(args)
self.writer:write(args.Key .. " = ") --<-- error is here, when trying to access args
if args.Type == "seqstrings" then
for k,v in pairs(args.Value) do
self.writer:write("\"" .. v .. "\" ")
end
elseif args.Type == "string" then
self.writer:write("\"" .. args.Value .. "\"")
elseif args.Type == "seqnumbers" then
for k,v in pairs(args.Value) do
self.writer:write(tostring(v) .. " ")
end
elseif args.Type == "number" then
self.writer:write(tostring(args.Value))
elseif args.Type == "boolean" then
if args.Value == true then
self.writer:write("1")
elseif args.Value == false then
self.writer:write("0")
end
end
self.writer:write("\n")
end
function odfWriter:close()
if self[open] then
self.writer:close()
self.writer = false
self[open] = false
else
error("tried to close an already closed writer")
end
end
loopSetup.lua
-----------------------------------------------------------------------
-- file : loopsetup.lua
-- description : provides global access to all of the (known) members
-- of the loop.simple code (for easier access)
-----------------------------------------------------------------------
require 'loop.simple'
class = loop.simple.class
classof = loop.simple.classof
initclass = loop.simple.initclass
instanceof = loop.simple.instanceof
isclass = loop.simple.isclass
memberof = loop.simple.memberof
members = loop.simple.members
new = loop.simple.new
rawnew = loop.simple.rawnew
subclassof = loop.simple.subclassof
superclass = loop.simple.superclass
ioWriter.lua:
local loaded = require('loopsetup')
assert(loaded, 'loopsetup not loaded')
local open = {}
ioWriter = class{
stream = false
}
ioWriter[open] = false
function ioWriter:open(name)
if not self[open] then
self.stream = io.open(name, "w")
self[open] = true
else
error("attempted to open an already open writer")
end
end
function ioWriter:write(str)
self.stream:write(str)
end
function ioWriter:writeLine(str)
self.stream:write(str .. '\n')
end
function ioWriter:close(self)
if self[open] then
self.stream:flush()
self.stream:close()
self.stream = false
self[open] = false
else
error("attempted to close an already closed writer")
end
end
test code:
require 'loopsetup'
require 'odfWriter'
local odf = odfWriter()
odf:open('test.odf')
local line1Data = {
Type = "seqstrings",
Key = "engineTargetHardpoints",
Value = {"hp01", "hp02", "hp03"}
}
odf:write(line1data)
odf:close()
Why do i have this error when i am clearly passing in a valid table to odfwriter.write?
At least in your test code, you have a typo:
line1data ~= line1Data
You also have a typo in ioWriter.lua in the close method:
function ioWriter:close(self)
should be
function ioWriter:close()
You haven't explicitly checked that everything implementing odf:open() succeeded. My concern is that it looks like the whole flow control in of odf:open() seems to assume that everything succeeded. Is it possible that it didn't, and as a result that at the line indicated the error is caused by attempting to index self.writer containing nil?
It could be nil as opposed to false if odfWriter:open() didn't successfully execute the constructor self.writer = ioWriter() for example. I'm not a regular user of loop, so I might be barking up the wrong tree, but...
If that were happening, it would be easy to get a message that is confused about which index was at fault.
Perhaps dropping calls to assert() in a few choice spots would be productive.

Resources