Press a button and text increase +1 in roblox studio - lua

could someone help me with this error?
I have a button created called "TextButton" and I have a text called "TextLabel", what I need to do is make the "TextLabel" increase by 1 when I press the "TextButton" button, could someone help me with this please.
function script.Parent.MouseButton1Click:Connect(function()
print("working..?..")
script.Parent.ValorVoto.Value = script.Parent.ValorVoto.Value+1
script.Parent.Parent.TextLabel = "clicks: "..script.Parent.ValorVoto.Value
end
script.Parent.Parent.TextButton.MouseButton1Down:Connect(clicking)

I think you've got a syntax error. It looks like you're defining a function, but also trying to connect that function immediately to the TextButton. Try naming your function clicking, and then passing that into the connection.
function clicking()
print("working..?..")
local vv = script.Parent.ValorVoto
vv.Value = vv.Value + 1
script.Parent.Parent.TextLabel.Text = "clicks: " .. tostring(vv.Value)
end
script.Parent.MouseButton1Click:Connect(clicking)

You have 2 errors in this script:
Line 4, add a .Text after referencing the TextLabel.
As Kylaa said, there is an error with line 7, you can just get rid of that line because your function was called with an event already. You don't need to recall that function, plus the function you're trying to call doesn't exist.
You don't mark it as a function if it's already listening for an event.
script.Parent.MouseButton1Click:Connect(function()
print("working..?..")
script.Parent.ValorVoto.Value = script.Parent.ValorVoto.Value+1
script.Parent.Parent.TextLabel.Text = "clicks: "..script.Parent.ValorVoto.Value
end

Related

roblox LUA Expected 'end' (to close 'than' at line 3), got '='

function teleportTo(placeCFrame)
local plyr = game.Players.LocalPlayer;
if plyr.Character then
return plyr.Character.HumanoidRootPart.CFrame = placeCFrame;
end
end
teleportTo(game:GetService("Workspace").game:GetService("Workspace").Zeppelin.FuelTank1.Tank.CFrame)
my code is here, idk much about coding thanks for helping
and if you told me how to make the player teleport to a moving object it would be so super
The error is telling you what is going on.
When the code was being interpreted line by line, it expected the next symbol to be end, but instead it got =.
That means that something about how you're using the equals sign is incorrect. So when we look at the line :
return plyr.Character.HumanoidRootPart.CFrame = placeCFrame
You cannot assign a value on the same line as a return command. return is used to pipe a value out of a function so it can be used where the function was called.
But I'm pretty sure that isn't what you intended, you just want to set a player's position. So to fix your issue, remove the return.
if plyr.Character then
plyr.Character.HumanoidRootPart.CFrame = placeCFrame
end
The reason you are getting an error is because = is usually defined as setting a value, and no code can be executed after a return or it will error
If you wanted to, you could add return after all the code is done executing

What does "attempt to index function with 'Connect'" mean in Roblox?

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.

PICO-8 making a button press show an output of text only once?

I'm a complete newbie to both Lua, PICO-8, and coding in general. I'm having trouble with a function I want to put in my first program. The text is all placeholder, I will change it once I get the code right and comprehend it.
Basically, before the _init() I have a function ow() defined where I press a button and the program displays the text "ow." I put the function name in _update() so that it will update 30x/second to see if the button is pressed; however, this is making the "ow" appear 30 times a second (or however long the button is pressed) instead of appearing once when I initially press the button. How do I fix this? Thank you for your tolerance of a new coder's question in advance. Here's my code:
function ow()
if btn((X))
then print "ow"
--how do i make it do this
--only once?
end
end
function _init()
print "hello."
print "i have been waiting for you."
end
function _update()
ow()
end
function _draw()
end
You need a global variable to save previous status of the button.
function ow()
if btn((X)) then
if not button_was_pressed then
button_was_pressed = true
print "ow"
end
else
button_was_pressed = false
end
end

ROBLOX Studio function to call function for each door to open

I'm pretty new to ROBLOX development and don't really know how to make the objects talk just yet.
I have 16 cells all with an individual open and close button. Those work. I want to create a button that will open all doors at once.
function onClicked()
script.Parent.Parent.Door.Transparency = 1
script.Parent.Parent.Door.CanCollide= false
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
The above function is on each cell and works. I would like to loop through each one and fire it when I click a different button. I've been looking into getting each object with the same names but haven't been able to iterate through it.
The below code is my attempt to get it to fire off each one!
local part = workspace.OpenButton
local clickDetector = Instance.new("ClickDetector")
local function onMouseClick(player)
for _, child in pairs(workspace.PrisonCell:GetChildren()) do
print(child)
child:connect(child.Door.onClicked)
end
end
clickDetector.Parent = part
part.Parent = workspace
clickDetector.MouseClick:connect(onMouseClick)
Any help with this would be greatly appreciated!
You could do something similar to this to open all of the prison doors at once:
local part = workspace.OpenButton
local clickDetector = Instance.new("ClickDetector")
local function onMouseClick(player)
for _, child in pairs(workspace.PrisonCell:GetChildren()) do
child.Door.Transparency = 1
child.Door.CanCollide = false
end
end
clickDetector.Parent = part
part.Parent = workspace
clickDetector.MouseClick:connect(onMouseClick)
The downside to using the solution above is that if you want to change the door opening script, you would have to change it in both the individual cell buttons as well as this master button script. If you think this might be a problem in the future, I would consider writing a master script somewhere in ServerScriptService that uses BindableEvents for communcation with the button clicking scripts in order to keep the door opening function in one place.

I can't get my script that teleport's me then closes the textbox to work

What I am trying to do is the following : The player loads up my game, is greeted by 2 Text buttons asking, explore or play campaign, when they click for example 'Campaign' it teleport's them to the campaign start and then shuts the game menu, i am very new to Roblox Lua and can't get this script to work, please help!
script.Parent.MouseButton1Click:connect(function()
game.Players.LocalPlayer.character.LowerTorso.CFrame = CFrame.new(workspace.CampainSpawn.Position)
end)
function onClick()
Parent.Parent.Visible = false
end
The problem with your code is that you are not calling the onClick() function therefore it will never execute the code which makes your game menu invisible. Please tell me if the code below works for you:
script.Parent.MouseButton1Click:connect(function()
game.Players.LocalPlayer.character.LowerTorso.CFrame = CFrame.new(workspace.CampainSpawn.Position)
script.Parent.Parent.Visible = false
end)
If the code above does not work, please add a picture of the structure of the user interface in your original post as that will help me out a lot more.

Resources