How do I assign a variable to a thing in roblox? - roblox

There is a variable called f, And this code is supposed to play random audio whenever it's clicked
local clickDetector = script.Parent.ClickDetector
f = math.random(1,3)
function onMouseClick()
script.Parent.f.TimePosition = 0
script.Parent.f.Playing = true
end
clickDetector.MouseClick:connect(onMouseClick)
How do I make it so it will thing it's not inside

assuming the parent exist, try
script.Parent[f]

Related

Error in save as type using uiputfile - Matlab

In app designer I have two buttons one to declare the working folder:
function setSaveLocationButtonPushed(app, event)
app.path = uigetdir()
end
The other one to save an image
function saveButtonPushed(app, event)
pathSave = app.path;
[file, pathSave] = uiputfile([pathSave,'*.jpg']);
…
end
Why do I get in the save as type also the app.path? (as shown in image)
Your code [pathSave,'*.jpg'] concatenates the path and the filter and then passes the result as the only argument to the uiputfile function. This argument tells the function what file filter to use.
Instead of storing the chosen directory, make it change the current directory. The file selection UI always opens in the current directory.
function setSaveLocationButtonPushed(app, event)
p = uigetdir;
cd(p)
end
function saveButtonPushed(app, event)
[file, pathSave] = uiputfile('*.jpg');
…
end
If you don’t want to change the current directory for the whole application, you can change it just before calling the uiputfile function, and change it back afterward:
function saveButtonPushed(app, event)
p = cd(app.path);
[file, pathSave] = uiputfile('*.jpg');
cd(p);
…
end

How to make a Roblox script search a player's backpack

I am trying to make a script that searches a players backpack when they touch a door, so it can tell if the player has a key card. If the player has a key card, it should say "Yes", but for some reason it keeps bringing up an error. Here is my code:
function onTouched(m)
p = m.Parent:findFirstChild("Humanoid")
if p ~= nil then
n = p.parent
local letin = game.Players(n).Backpack:FindFirstChild("Key Card")
if letin then
print("Yes")
else
print("No")
end
end
end
script.Parent.Touched:connect(onTouched)
The error is:
Trying to call method on object of type: 'Player' with incorrect arguments.
Does anyone know why this might not work?
I think you've got two problems :
It looks like you are trying to access an array index, but you're using () instead of [].
The game.Players object is a service class, not an array. But you can call game.Players:GetPlayers() to get that array of the players.
Since you are already getting the player object, you can simply grab the player's name and use that to lookup the player from game.Players.
Your script just about works, here's a fix for you :
function onTouched(m)
-- get the player in game that touched the thing
local p = m.Parent:findFirstChild("Humanoid")
if p ~= nil then
-- grab the player's name
local n = p.parent.Name
-- find the player in the player list, escape if something goes wrong
local player = game.Players:FindFirstChild(n, false)
if player == nil then
return
end
-- search the player's backpack for the keycard
local keycard = player.Backpack:FindFirstChild("Key Card")
if keycard then
print("Yes")
else
print("No")
end
end
end
script.Parent.Touched:connect(onTouched)
The following line of code is incorrect, because instead of finding a child in
'game.Players', you call it as a function by using parenthesis.
local letin = game.Players(n).Backpack:FindFirstChild("Key Card")
What you probably intended was:
local letin = game.Players[n].Backpack:FindFirstChild("Key Card")
A more clean approach to getting that 'Key Card' is to also check if 'n' really is the name of a player, rather than that of a NPC.
local player = game.Players:FindFirstChild(n)
if player then
local letin = player.Backpack:FindFirstChild("Key Card")
print(letin)
end

Leaderboard-Class instead of KO in Roblox

i'm creating a game at the moment and i need to know to add a section into the leaderboard like how the KO system works, however i need it to display names of the classes, such as "Wizard" etc. Making them into teams instead makes it too crowded and this would help.
You can add multiple types of values to your leaderboard (leaderstats) object by putting different value objects. For example, if you want to have titles on the leaderboard, you put a StringValue into your leaderstats object.
Code:
local players = game:GetService("Players")
function playerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local title = Instance.new("StringValue")
title.Parent = leaderstats
title.Name = "Class"
title.Value = "Wizard" -- or whatever you want it to be
end
players.PlayerAdded:Connect(playerAdded)
To change the class, all you have to do is change the value of the StringValue Class.
player.leaderstats.Class.Value = "Warrior"
References:
https://www.robloxdev.com/articles/Leaderboards
https://www.robloxdev.com/api-reference/event/Players/PlayerAdded
https://www.robloxdev.com/api-reference/class/StringValue

Set object properties after setting the object

I've created a class with various properties in VB6.
The properties are
PiccoId
OrderId
UserId
StockCode
Quantity
At the top of the class I have declared 2 instances of classes I'm using.
Dim stkLine As CSOPSLine ' This is the class where the properties are declared and read
Private SOPSLines As cSLine ' This class creates the new objects and sets the property values
When the user enters the order number on a handheld barcode scanner, I'm creating an object to associate with this particular scanner like so:
Set SOPSLines = New cSLine
Set SOPSLines = getSOPSLine(ID, sOrder, "", "", 0)
In the next stage of the process, the user needs to enter their user ID so it can be seen which user scanned in the item.
I need to update the property of this same object, but I'm not sure how - Currently I am trying to do it within my getSOPSLine function, like this:
Dim line As New CSOPSLine
Dim bFound As Boolean
bFound = False
For Each line In SOPSLines.Items
If line.PiccoId = ID Then
line.OrderId = OrderId
line.Quantity = Qty
line.StockCode = stock
line.UserId = UserId
Set getSOPSLine = line
bFound = True
Exit For
End If
Next
If bFound = False Then
Set line = SOPSLines.Add(ID, OrderId, UserId, stock, Qty)
Set getSOPSLine = line
End If
Set line = Nothing
However, as I'm not storing sOrder at class level (Due to the fact multiple users can be using barcode scanners, the order ID can't be kept at class level as other users will just overwrite it),
I'm not sure how once I've got the next variable (In this case, userID, and the other variables after this stage) I can update the same object as I've just created.
I've tried to do something like
Set stkLine = getSOPSLine(ID, stkLine.OrderId, pUser, "", 0)
but it errors saying
object or with block variable has not been set
How can I update the properties of stkLine without constantly creating new objects?
EDIT
To clarify:
When the application receives data from the handheld barcode scanner, a select case is entered, with one case for each of the variables being entered (E.g. Order ID, user ID, stock code etc.)
The first case sets the order ID.
The code here is
Case FRAME_ORDER_SELECTION
On Error Resume Next
Dim sOrder As Long
sOrder = Picco.GetData(ID, 50)
If sOrder = 0 Then
Call Picco.Send(ID, FRAME_ORDER_SELECTION)
Exit Sub
Else
With Picco
Call .ClearForm(ID)
Call .Text(ID, LINE_1, "===== User ID =====")
Call .Text(ID, LINE_2, "")
Call .NewField(ID, 60, 5, FLD_LINE + SND_ENTER)
Call .Send(ID, FRAME_LINE_ADD)
Set SOPSLines = New cSLine
Set SOPSLines = getSOPSLine(ID, sOrder, "", "", 0)
End With
End If
frameid = FRAME_LINE_ADD
m_iLastFrameId = FRAME_ORDER_SELECTION
On Error GoTo Picco_DataArrived_Err
This is where the object is created.
The next case is after the user enters their user ID into the scanner.
Case FRAME_LINE_ADD
On Error Resume Next
Dim pUser As String
pUser = ""
pUser = Picco.GetData(ID, 60)
If pUser = "" Then
Exit Sub
End If
On Error GoTo Picco_DataArrived_Err
With Picco
Call .ClearForm(ID)
Call .Text(ID, LINE_1, "===== Add Line =====")
Call .Text(ID, LINE_2, "")
Call .Text(ID, LINE_7, "Scan or type code")
Call .NewField(ID, FIELD_POS, 18, FLD_LINE + FLD_READER + SND_ENTER)
Call .Send(ID, FRAME_LINE_QTY)
End With
Set stkLine = getSOPSLine(ID, stkLine.OrderId, pUser, "", 0)
frameid = FRAME_LINE_QTY
m_iLastFrameId = FRAME_LINE_ADD
Then there will be 2 or 3 more cases when populating the rest of the required values.
What I need to do, is in the second case (And all other following cases) update the properties of the object created in the first case.
I'm using the getSOPSLine function as this gets the object with the matching barcode scanner ID (As multiple users may be accessing different orders, they need to be kept separate in this way), but I'm not sure how to update the object where the scanner ID matches.
When you call getSOPSLine in each Case, enter some temporary values for the variables that you're not setting.
Example;
In the UserID case: stkLine = getSOPSLine(ID, 0, pUser, "", 0)
Then, in the getSOPSLine() function, change it so that instead of setting the values automatically, it instead only updates them if they don't equal 0, or "", or whichever temporary variables you use.
That way you're updating the same object, but aren't storing data that may get overwritten.
I'm not sure what you're confused about; I think that you must have some misunderstanding of what objects, variables, and properties are that is preventing you from asking this in a way that I understand. I'm not sure how your code lines up with your questions. But I'll give this a shot:
Private SOPSLines As cSLine
Set SOPSLines = New cSLine
Set SOPSLines = getSOPSLine(ID, sOrder, "", "", 0)
This declares a class-level variable, that can hold a cSLine object. That seems reasonable. Then you create a new instance of cSLine to put in that variable. But I don't know why, because you then point the variable to a different instance entirely, which is the instance returned from your getSOPSLine function. That middle line doesn't seem to be doing anything, and I'm not sure what you're trying to do with it.
I've tried to do something like
Set stkLine = getSOPSLine(ID, stkLine.OrderId, pUser, "", 0)
but it errors saying
object or with block variable has not been set
Well, then it sounds like stkLine isn't set to an object. I don't see any code that's trying to set stkLine other than that line. So when you try to get stkLine.OrderId, there isn't an object to get the OrderID property of, so it gives you an error.
I need to update the property of this same object, but I'm not sure how
Well, if there's an object you care about, you probably have it in a variable somewhere. You use variable.property = value syntax to assign value to the property of the object currently stored in variable.
But again, I'm not sure where your confusion is, since you're clearly assigning properties of objects in your code already.
If you're not understanding how objects work in VB, I'd recommend reading through the Working with Objects chapter of the documentation.
And if your confusion lies elsewhere, perhaps you could put together a more specific question about what you're trying to do?

Swift: Indirect access / mutable

I need to go to a referenced structure:
class SearchKnot {
var isWord : Bool = false
var text : String = ""
var toNext = Dictionary<String,SearchKnot>()
}
When inserting, I need to update values in toNext dictionary. Because I want to avoid recursion, I do it in a loop. But there I need a variable which jumps from one toNext item to the other, able to change it.
var knots = toNext
...
let newKnot = SearchKnot()
knots[s] = newKnot
The last command only changes a local copy, but I need the original to be changed. I need an indirect access. In C I would use *p where I defined it as &toNext. But in Swift?
I found a solution. I remembered old pascal days. ;-)
I don't use the last reference, but the second last. Instead of
knots[s]
I use
p.knots[s]
For hopping to the next knot, I also use
p = p.knots[s]
and could use
p.knots[s]
again. Also p.knots[s] = newKnot works, because p is local not the entire term.