#Scripting Question
1 messages · Page 1 of 1 (latest)
So you want the player to be assigned a random number everytime they join that game?
yeah
wait no
not random in order
like if ur the fifth person in the server u get the number 5 but if somebody leaves u still have that number
Simply get the player counts currently in game and assign it to player when joined
you need a script that constantly changes player's numbers then reassinging them whenever a player leaves or joins the game
do you specifically need them in order by when they joined your game
or just AN order
yeah
alright i gotchu
Really easy
In a server script make a table called playercounts or sum
Then whenever a player joins
[#playercounts+1] = player.userid
When player removing
Table.remove(playercounts,player.userid)
There might be errors in this very. Very simple code but like u get the point
issue is say 3 people join and the second leaves
that just leaves a gap at 2
3 doesnt become 2
local Players = game:GetService("Players")
local playerJoinList = {}
local function updateJoinNumbers()
for i, player in ipairs(playerJoinList) do -- loops through the players in the playerJoinList
local joinNumber = player:FindFirstChild("JoinNumber") -- find the IntValue saved on the player
if not joinNumber then --// if it doesnt already exist
joinNumber = Instance.new("IntValue") --// create an intvalue
joinNumber.Name = "JoinNumber" --// name it JoinNumber
joinNumber.Parent = player --// parent it to the player
end
joinNumber.Value = i --// set it to the next avaliable order number
end
end
Players.PlayerAdded:Connect(function(player) --// when a player joins
table.insert(playerJoinList, player) --// insert them into the list (LIST, player)
updateJoinNumbers() --// fire the updateJoinNumbers function
print(playerJoinList) --// print the list
end)
Players.PlayerRemoving:Connect(function(leavingPlayer) --// when a player leaves
for i = #playerJoinList, 1, -1 do --// i = the length of how many people are in game, count down by one
if playerJoinList[i] == leavingPlayer then --// playerjoinlist[i] is the player that is leaving
table.remove(playerJoinList, i) --// remove them from the list
break
end
end
updateJoinNumbers() --// reupdate the list so it doesnt leave gaps
print(playerJoinList) --// print the list
end)
ive commented it so you can understand
try not to just copy and paste
https://create.roblox.com/docs/reference/engine/libraries/table#remove
it removes the specified value and moves everything correctly so 3 would become 2
So this should be in player removing
Table.remove(playercounts, table.find(playercounts, player.UserId))
youll notice in the player object "Player2" is just me 9INE
there is now an intvalue
with a value of 1
any questions shoot
Just seems a bit overcomplicated
ill show you why i do it that way
local Players = game:GetService("Players")
local playercounts = {}
Players.PlayerAdded:Connect(function(player)
playercounts[#playercounts + 1] = player.UserId
print("Player joined:", player.Name, "UserId:", player.UserId)
print(playercounts)
end)
Players.PlayerRemoving:Connect(function(player)
table.remove(playercounts, player.UserId)
print("Player left:", player.Name, "UserId:", player.UserId)
print(playercounts)
end)
woah, if only scripting was this simple to understand
scripting isnt that difficult if you take it step by step
when you look at a whole script 1000 lines you go GYATT
but when you read line by line its like oohhhhh
yeah, i know, im tryna fix my own script, i asked in this discord a bit ago
aslong as it isnt to do with cars planets then i can help lol link it
or guns ideally since i havent done those 😭
Cars are just a lot of math, nothing more to be frank
they often use services like what was it render stepped?
im not to far into those atm
just stepped, or well, renderstepped would be a bit odd. They tend to just use them for the delta time. So they can base the spring velocity off of that
Basically, if you don't divide, then a lag in frames can cause your car to shoot up (as it tries to apply more force over a longer time)
(or less force, I cannot remember the exact formula to be frank, would've to look at my old code to remember it)
see what i mean lol 😭 i dont often do help with cars or id say yeah try use heartbeat service and apply bodyvelocity to the car 
Yup u got the count on the left and the players id on the right
the issue is it didnt update when the player left
Show me script
Been a bit since I've used luau's tables, but it can very well be that it assumes that this is a dict instead of an array
Nevermind, I just looked up table.remove, you forgot to add in a table.find
local Players = game:GetService("Players")
local playercounts = {}
Players.PlayerAdded:Connect(function(player)
playercounts[#playercounts + 1] = player.UserId
print("Player joined:", player.Name, "UserId:", player.UserId)
print(playercounts)
end)
Players.PlayerRemoving:Connect(function(player)
table.remove(playercounts, player.UserId)
print("Player left:", player.Name, "UserId:", player.UserId)
print(playercounts)
end)
table.remove(playercounts, table.find(playercounts, player.UserId))
@pliant pine you think this is good or overcomplicated
It may be undercomplicated depending on the solution
It might not work i don’t know btw
He said "so the spot clears up", not sure if he means that 2 will turn into 1, or that the next join gets 1
use a fifo queue. you will have to account for dangling player pointers but that's just the way things go 
In computing and in systems theory, first in, first out (the first in is the first out), acronymized as FIFO, is a method for organizing the manipulation of a data structure (often, specifically a data buffer) where the oldest (first) entry, or "head" of the queue, is processed first.
Such processing is analogous to servicing people in a queue a...
im trying with hermans idea
It should make 2 become one
sounds more he means that the next should be the max index + 1
wait for op at this point, it's only been about 30 minutes.
True, this is another one of those cases were the solution truly depends on what they meant
my script does seem to work
well it depends on the solution, if he wanted player4 to keep index 4 then it doesn't
wait im back at my pc let me cook
also true, but also also this is all a solved problem, you need some overflow if you want to account for players disconnecting (forgiving some time to 'fully disconnect') compare to new players joining (userid overflow), so your userid limit must be greater than your userid limit. thems just the way things go 
Or a simpler solution: plr:kick() the people that left
Just add them to a datastore and kick everybody that joined for a second time
We all know the easiest way to remove edge cases is by just deleting the entire scenario that sets them up, right?
aka player 4 can take original-p4's slot while p4 is waiting to fully-disconnect (including kick, which has probably already triggered join queries by then), but if you want to do anything with formerly-p4 then you need to overflow the player userid limit.
this is always the easiest way, problem is, it's not always easy to set up clean delete-the-entire-thing scenarios 
@fleet steeple it works for me
local Players = game:GetService("Players")
local PlayerCount = {}
Players.PlayerAdded:Connect(function(player)
PlayerCount[#PlayerCount +1 ] = player.UserId
print(PlayerCount)
end)
Players.PlayerRemoving:Connect(function(player)
local index = table.find(PlayerCount,player.UserId)
print(index)
table.remove(PlayerCount,index)
print(PlayerCount)
end)
I did find an easy solution that has so far worked 100% of the time
perfect just make sure you know why its doing what its doing
||just stop writing code for games||
looks good, just be safe around the fact that the # operator won't return a valid result since you have nils in the array (not contiguous)
the only thing that might need explaining is #PlayerCount just means the length of the PlayerCount list
so if PlayerCount = {"Gabe" : 1, "Herman" : 2, "Pyro" : 3}
#PlayerCount just means 3 because thats how many entities are in the list
wasn't there some maxn function in lua arrays that just bypasses that issue
Could be luau or just have a different name
this is indeed a thing, and very much applies here https://create.roblox.com/docs/reference/engine/libraries/table#maxn
My brain may have forgotten half of the syntax already (I've had to open some online lua compiler to check if some of the shit I was saying was actually true), but at least I recall some obscure parts of luau
looks like it used to be a lua thing but it's been deprecated https://www.lua.org/manual/5.1/manual.html
table.maxn (table)
Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To do its job this function does a linear traversal of the whole table.)
https://stackoverflow.com/questions/31432034/missing-functions-in-table-library-in-lua
table.getn was deprecated in lua 5.1 (ref)
table.maxn was deprecated in lua 5.2 (ref)
https://www.lua.org/manual/5.2/manual.html#8.2
Function table.maxn is deprecated. Write it in Lua if you really need it.
isn't luau based off of 5.1, so then it is not deprecated in the version roblox uses. Main thing I now wonder is: did lua actually fix their shitty arrays?
i have no idea, roblox has done a number on both lua and luau.
easier to treat roblox luau as it's own flavor of luau, which is itself a flavor of lua. 
Wait, there's a difference between luau and roblox' luau compiler?
(other that just engine libraries and API shit)
That sounds dumb, but it does sound like something roblox would do
swear yall tryna right a S4 ahh script for this 😭
i get it your accounting for alot more than the surface
? we're just talking about luau arrays, not really about the problem itself anymore