How do I assign usernames to a ROBLOX Team Spawner? - roblox

I would like to know how to assign certain usernames to a team spawner.
Basically when a certain username joins the game, they get placed into a team on the leader board and spawn at the correct spawn in game.

You can do this by checking the username and assigning the team. So something like this, assuming you don't have teams already set up.
team = Instance.new("Team",game:GetService'Teams')
game:GetService'Players'.PlayerAdded:connect(function(p)
if p.Name == "name" then
p.Team = team
end
end
Obviously you can set the team name and color through this script or create it your own.

You have to do the player handling yourself. Basically you want to listen on new players with PlayerAdded and set the team (based on some kind of magical function) with Team.
However you can assign Spawners to a team by setting the TeamColor to equal the Team's TeamColor.
Example code:
local Teams = game:GetService("Teams")
local RedTeam = Teams["Red Team"]
local OtherTeam = Teams["Other Team"]
local PlayersInTeamRed = { "Player1" , "Player2" }
game:GetService("Players").PlayerAdded:connect(function(player)
for playerName in ipairs(PlayersInTeamRed) do
if player.Name == playerName then
player.Team = RedTeam
return
end
end
-- Maybe another list here
-- not in "Team red"
player.Team = OtherTeam
end)
Note that you might want to check against the UserId instead, because some players changes their name.
Also make sure that the Spawn's Neutral value is false.

Related

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?

How to generate different attributes everytime

In my code , the 100 stores are sharing the same attributes.
I thought it should create a different one every time.
(1..100).each do
store_attr = FG.attributes_for :store
store_attr[:account] = accounts.sample
stores << Store.create(store_attr)
end
FactoryGirl.define do
factory :store do
name Faker::Company.name
latitude 1.5
longitude 1.5
street Faker::Address.street_address
city Faker::Address.city
state Faker::Address.state
zip_code Faker::Address.zip_code
phone Faker::PhoneNumber.cell_phone
email Faker::Internet.email
website "https://#{Faker::Internet.domain_name}"
account nil
factory :complete_store do
name 'store_with_account'
account
end
end
end
I believe you need to put them inside a block -
street { Faker::Address.street_address }
Otherwise they will only be generated once and reused for all instances. You can read more here.

Meteor - alter data on publish

I am doing a card game in Meteor. Each played game is registered in the collection "Games".
A Games' record contain some of the game info as well as some players info and the cards hand of the players (players[i].cards, with values like 4S - 4 of spade, KH - king of heart...). Depending on the cards value, I can display the correct card to the frontend (div.card-KH).
Most of the game data is public but, of course, only the current player's cards must be visible.
One option could be to exclude the cards of the other players at publish time but I still need the number of cards to display the decks (only the remaining cards, hidden) of the other players.
Is there a way to replace the card value of each cards (except user's ones) in players[i].cards to, say, BACK (div.card-BACK) at publish time? Else, what would be the good design pattern to do this in Meteor?
Interesting question which I think there should be an official guideline to.
They have a bit in the docs about publishing:
http://docs.meteor.com/#/full/meteor_publish (secretinfo field only published to admins)
You could do the same but you would have to check the userId and have a player1secretinfo, player2secretinfo etc. (or perhaps a secretinfo array/object but then you would have to only publish one particular index/property)
You could also do a transform in the publications:
https://www.eventedmind.com/items/meteor-transforming-collection-documents
Please post it, if you find a good solution :)
Here is the only solution I found (I don't really like it but it works) :
Basically, you have to store a temporary document, with the same id but modified data, to another Collection, publish that document from that Collection and finally, from client side, subscribe to that publication.
Of course, never make hidden part of the original document available through another publication.
Meteor.publish('gameWithHiddenCards', function (gameId) {
var self = this;
//Transform function
var transform = function(item) {
for (var i=0; i<item.players.length; i++){
if ((item.players[i].id != self.userId))
for (var j = 0; j < item.players[i].cards.length; j++)
item.players[i].cards[j] = 'back';
return item;
};
/* 1. Observe changes in game ;
2. Apply transform function on game data ;
3. Store it to another Collection. */
var observer = Games.find({_id: gameId}).observe({
added: function (document) {
self.added('secure_games', document._id, transform(document));
},
changed: function (newDocument, oldDocument) {
self.changed('secure_games', newDocument._id, transform(newDocument));
},
removed: function (oldDocument) {
self.removed('secure_games', oldDocument._id);
}
});
self.onStop(function () {
observer.stop();
});
// Return altered game
return SecureGames.find({_id: gameId});
});