im having troubles getting my toggle script working in roblox - lua

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!

Related

Is there a way to Disconnect the Function from UIS.InputBegan:Connect(...)?

I tried everyway to somehwo make this work... It works on the first time you execute it, but if you try to execute it again it just fails. Here is the code:
script.Parent.MouseButton1Down:Connect(function()
KS.Visible = true
KSIO = true
wait(.5)
UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
if KSIO == true then
script.Parent.Text = Input.KeyCode.Name
KS.Visible = false
KSIO = false
end
print(KSIO)
end)
end)
If you want to disconnect the event, then you can do something like this to disconnect the event.
local conn
function handle(parm1, parm2)
-- logic goes here
if somethingistrue then
conn:Disconnect()
end
end
conn = UIS.InputBegan:Connect(handle)
You want to look at globals. Globals in lua are variables that can be accessed from another thread.
You might be looking for this.
if getgenv().HasRanBefore then return end
if not getgenv().HasRanBefore then getgenv().HasRanBefore = true end
script.Parent.MouseButton1Down:Connect(function()
KS.Visible = true
KSIO = true
wait(.5)
UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
if KSIO == true then
script.Parent.Text = Input.KeyCode.Name
KS.Visible = false
KSIO = false
end
print(KSIO)
end)
end)

How to make a base system? -- Roblox Lua

I'm trying to make a system to which when the player joins they are given a base and then no other players can use that base.
Here is my module inside the parented to the script:
local Functions = {}
function Functions.findOpenBase(plr)
local bases = workspace.Bases
for i,v in pairs(bases:GetChildren()) do
if v:IsA("Part") then
print("Searching..")
if plr.alreadyOwnsBase.Value == false then
if v.Owner.Value ~= nil then
print("Base found!")
v.Owner.Value = plr.Name
plr.alreadyOwnsBase.Value = true
else
warn("error")
plr:Kick("error finding base, Please Rejoin.")
end
end
else
print("cannot claim another base")
end
end
end
return Functions
then here is my handler script:
local module = require(script.Functions)
game.Players.PlayerAdded:Connect(function(plr)
local alreadyOwnsBase = Instance.new("BoolValue", plr)
alreadyOwnsBase.Name = "alreadyOwnsBase"
alreadyOwnsBase.Value = false
if plr then
module.findOpenBase(plr)
print(plr.Name)
end
end)
is there any solutions?
To me, module inside the parented to the script:
local Functions = {}
function Functions.findOpenBase(plr)
-- check it first.
if plr.alreadyOwnsBase.Value == false then
local bases = workspace.Bases
for i,v in pairs(bases:GetChildren()) do
print("Searching..")
if v:IsA("Part") then
if v.Owner.Value ~= nil then
print("Base found!")
v.Owner.Value = plr.Name
plr.alreadyOwnsBase.Value = true
-- get it, jump the loop
break
end
else
print("cannot claim another base")
end
end
end
-- and plr.alreadyOwnsBase.Value is the flag of the result
if plr.alreadyOwnsBase.Value == false then
warn("error")
plr:Kick("error finding base, Please Rejoin.")
end
end
return Functions
and the handler script:
local module = require(script.Functions)
game.Players.PlayerAdded:Connect(function(plr)
local alreadyOwnsBase = Instance.new("BoolValue", plr)
alreadyOwnsBase.Name = "alreadyOwnsBase"
alreadyOwnsBase.Value = false
if plr then
module.findOpenBase(plr)
-- check the flag
if plr.alreadyOwnsBase.Value then
print(plr.Name)
end
end
end)

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)

Can anybody see what is wrong with this Roblox script?

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

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)

Resources