#how would I do something like this

98 messages · Page 1 of 1 (latest)

chilly lark
#

so I wanna make a name system for this game and im wondering how would I make it give a value a string from a table if that makes sense
so like it gives a string value a string that is a random selection from a table

#

@chilly lark

#

lol how dare u

#

😕

#

lol here is a example to do```lua
-- Define a table of possible names
local names = {
"Alice",
"Bob",
"Charlie",
"David",
"Eve"
}

-- Select a random name from the table
local randomIndex = math.random(#names)
local selectedName = names[randomIndex]

-- Use the selected name wherever you need it
print("The selected name is: " .. selectedName)

chilly lark
#

tysm

#

lemme test it out

chilly lark
#

for names

chilly lark
# chilly lark how would I add % chances
-- Define a table of possible names
local names = {
  "Alice",
  "Bob",
  "Charlie",
  "David",
  "Eve"
}

-- Shuffle the list of names
for i = #names, 2, -1 do
  local j = math.random(i)
  names[i], names[j] = names[j], names[i]
end

-- Select the first name in the shuffled list
local selectedName = names[1]

-- Use the selected name wherever you need it
print("The selected name is: " .. selectedName)
```for example but not like the way i wanted to do lol
#

I dont fully understand that

#

welp here is what i made yestday for a random num pic```lua
-- Function to generate random numbers
local function generateRandomNumbers(numIterations, minValue, maxValue)
-- Set default values for minValue and maxValue
minValue = minValue or 1
maxValue = maxValue or 100

-- Initialize the table to hold the counts for each number
local counts = {}

-- Generate the random numbers and increment the count for each number
for i = 1, numIterations do
local rand = math.random(minValue, maxValue)
counts[rand] = (counts[rand] or 0) + 1
end

-- Return the table of counts
return counts
end

-- Example usage: generate 10,000 random numbers between 1 and 100 and print out the percentages
local counts = generateRandomNumbers(10000)
for i = 1, 100 do
local percentage = counts[i] and counts[i] / 10000 * 100 or 0
print(string.format("%d: %.2f%%", i, percentage))
end```

#

also if its possible can I make it so like if a players user id is = to (userid) then they get a set name

#

for custom names

chilly lark
#

oh I understand it

chilly lark
#

idk if that is the code

#

to make it work

#

Yeah you can definitely do that

#

I kinda just pulled that out of no where

#

Just do that outside of the generate numbers script

#

wait is selectedName a variable inside of the player?

#

or is it stored somewhere else

#

-- Define a table of possible names
local names = {
"Alice",
"Bob",
"Charlie",
"David",
"Eve"
}

-- Select a random name from the table
local randomIndex = math.random(#names)
local selectedName = names[randomIndex]
if game.players.localplayer.userid == "123456789" then
selectedName = Joe
end
-- Use the selected name wherever you need it
print("The selected name is: " .. selectedName)

#

like that ?

#

also no it isnt

#

yes something like that would work just fine

#

also thats what I have right now

#

but it just gives the player a random name

#

also it'd be more useful to make a local variable for player instead of constantly referencing it as game.Players.LocalPlayer

#

yeah I would do that in a real script I just did that for an example

#

Also put quotes around the name Joe

#

alr

#

but it seems that the script doesnt detect the userid in the real game

#

as I joined with a friend

#

and we both spawned with randomly generated names

#

I haven't tested this but it might work

-- Define a table of possible names
local names = {
"Alice",
"Bob",
"Charlie",
"David",
"Eve"
}

-- Select a random name from the table
if not plr.UserId = 123456789 -- replace this with your friends user Id or whatever
local randomIndex = math.random(#names)
local selectedName = names[randomIndex]
end
else
local selectedName = "Joe"
-- Use the selected name wherever you need it
print("The selected name is: " .. selectedName)

#

lemme test

bright mauveBOT
#

studio** You are now Level 4! **studio

chilly lark
#

dont put quotes around the number

#

and use 2 equal signs

#

wait actually let me remake it

#

gimme a sec

#

i completely failed on that one

chilly lark
#

work in progress

#

xd

#

so you want chances for the names?

#

I mean I did but it looks really complicated for me right now

#

it isn't that hard

chilly lark
#

also do either of u know how to get rid of this really annoying white bar thingy

#

it deletes anything when I try typing

#

press the insert button on your keyboard

bright mauveBOT
#

studio** You are now Level 9! **studio

chilly lark
#

ah thanks

chilly lark
#

infact i made many errors

#

yikes

#

ye i do that sometimes if i do it in discord but in roblox i get no errors

#

uh well idk what is wrong with it now

#

but its saying smth about x and y

#

so show full code

#

im still working on mine lol

chilly lark
# chilly lark

i dont think u have the correct thing lol and u can use a else not after a end

#

would this work?


local name = ""

game.Players.PlayerAdded:Connect(function(player)
local names = {
    "Jane",
    "John",
    "Erik",
    "David"
}

--Get a random name for player
if player.UserId ~= "123456789" then --change this to your user or whatever
    local randomIndex = math.random(#names)
        name = (names[randomIndex])    
        print("Your name is: "..name)
end
if player.UserId == "123456789" then --change this to your user or whatever
        name = "YourName"
    end
    print("Your name is: "..name)
end)
#

Nvm tried it and it didn't work

#

smhhhh

#

lol well im trying something out like with functions and other things

#

ah

chilly lark
# chilly lark would this work? ```lua local name = "" game.Players.PlayerAdded:Connect(fun...

try this```lua
local names = {
"Jane",
"John",
"Erik",
"David"
}

-- Function to generate a random name
local function getRandomName()
local randomIndex = math.random(#names)
return names[randomIndex]
end

game.Players.PlayerAdded:Connect(function(player)
local name = ""
-- Get a random name for the player, unless it's the specified user ID
if player.UserId ~= "123456789" then --change this to your user or whatever
name = getRandomName()
else
name = "YourName"
end
print("Your name is: "..name)
end)

#

I'm usually good with math related things and this is literally the easiest of math things

chilly lark
#

Name 1

#

then ill change to my user id after name 2

#

name 2

#

so that script works?

#

User Id part doesn't seem to work

#

still made my name erik

#

maybe it would be possible if instead of using the userid

#

you checked if the characters name = a specific name

#

@chilly lark you had it switched around

#

local names = {
"Jane",
"John",
"Erik",
"David"
}

-- Function to generate a random name
local function getRandomName()
local randomIndex = math.random(#names)
return names[randomIndex]
end

game.Players.PlayerAdded:Connect(function(player)
local name = ""
-- Get a random name for the player, unless it's the specified user ID
if player.UserId ~= "123456789" then --change this to your user or whatever
name = "Sally"
else
name = getRandomName()
end
print("Your name is: "..name)
end)

#

you made it where if the players user id was the user id then it randomly generated the name

#

and if it wasn't then it chose the set name

#

so it works ?

#

yh

#

just change sally to wtv name u want

#

and change the user id to yours

#

alr lemme test this out

#

tysm btw