#Scripting Question

1 messages · Page 1 of 1 (latest)

clear notch
#

How do I make a script that assigns the player a number based on when they joined the server? So if they were the second to join it would be 2, and if they left that number would be free again.

dusk haven
#

So you want the player to be assigned a random number everytime they join that game?

clear notch
#

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

green prairie
fleet steeple
#

you need a script that constantly changes player's numbers then reassinging them whenever a player leaves or joins the game

fleet steeple
#

or just AN order

clear notch
#

yeah

fleet steeple
#

alright i gotchu

barren oriole
#

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

fleet steeple
#

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

barren oriole
fleet steeple
#

youll notice in the player object "Player2" is just me 9INE
there is now an intvalue

#

with a value of 1

#

any questions shoot

barren oriole
fleet steeple
#

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)
dusk haven
#

woah, if only scripting was this simple to understand

fleet steeple
fleet steeple
dusk haven
fleet steeple
#

or guns ideally since i havent done those 😭

dusk haven
#

crouching

#

its labled Crouch not, crouching

pliant pine
fleet steeple
#

im not to far into those atm

pliant pine
#

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)

fleet steeple
#

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 Thumbs

barren oriole
# fleet steeple

Yup u got the count on the left and the players id on the right

fleet steeple
barren oriole
#

Show me script

pliant pine
#

Nevermind, I just looked up table.remove, you forgot to add in a table.find

fleet steeple
# barren oriole Show me script
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)
pliant pine
fleet steeple
pliant pine
barren oriole
#

It might not work i don’t know btw

pliant pine
#

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

midnight spear
# clear notch like if ur the fifth person in the server u get the number 5 but if somebody lea...

use a fifo queue. you will have to account for dangling player pointers but that's just the way things go fingerguns

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...

fleet steeple
#

im trying with hermans idea

fleet steeple
#

hermans idea seems to work

#

im not sure i can test adding a player again

pliant pine
fleet steeple
#

nope

midnight spear
pliant pine
fleet steeple
#

my script does seem to work

pliant pine
barren oriole
#

wait im back at my pc let me cook

midnight spear
pliant pine
#

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?

midnight spear
midnight spear
barren oriole
#

@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)
pliant pine
fleet steeple
pliant pine
#

||just stop writing code for games||

midnight spear
fleet steeple
#

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

pliant pine
#

Could be luau or just have a different name

pliant pine
midnight spear
# pliant pine Could be luau or just have a different name

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.

pliant pine
midnight spear
#

easier to treat roblox luau as it's own flavor of luau, which is itself a flavor of lua. pogmoai

pliant pine
#

(other that just engine libraries and API shit)

#

That sounds dumb, but it does sound like something roblox would do

fleet steeple
#

swear yall tryna right a S4 ahh script for this 😭

#

i get it your accounting for alot more than the surface

pliant pine