Javascript server side calls

i have been trying to fetch GetPlayers() from the server inside my tick and am getting a “no such function exists”
( NOTE: i am using Javascript, not LUA )
i IMAGINE it has something to do with the fact that SOME server functions dont trigger unless called from the main gameloop. i read that on some post, but was unsure if that applies here.

i’ve been crawling the docs for hours trying to figure it out, how do i simply call this like i would in lua?!?!

any assistance will stop me beating my head off this table.
i REALLY dont want to write a lua export to do something this simple…
( but that’ll be my ducttape fix. )

HALP! :smiley:

1 Like

For anyone reading this and wondering if i solved the problem

no, i did NOT figure out how to call
getplayers()
instead i called the json from
http://URL:PORT/players.json

and used that instead.
I’d still be curious if there’s another way to do it, but this seems to be the method i found to be most simple.

._.???

https://runtime.fivem.net/doc/natives/#_0x63D13184

https://runtime.fivem.net/doc/natives/#_0xC8A9CE08

yeah your answer is shinier…
here… have your trophy back…
:stuck_out_tongue:
Thanks! those’ll work nicely.

hey @magnesium and/or any other devs reading this.
i’m admitting i might not understand the JS part of the fivem implimentation,
do you have an example of how i’d simply call a list of connected players from server?
i tried converting the cookbook setups to no avail.
why is there no equiv to getplayers() in the JS engine? this would be SO MUCH easier…

I am no js genuis but cant u just do like you do in lua?

for (i = 0; i < GetActivePlayers().length; i++) {
	var ped = GetPlayerPed(i);
	// do stuff
}

After testing:

GetActivePlayers() dont seem to be a supported native in javascript.
I guess you can do that as a function in lua and send it to you server.js as an object. https://gyazo.com/ba94a1b4255ebbc8a56dae939faac13f

After more testing. Seems like GetPlayersActive() only can be called from client side. I just made a command to test, to get the players on server side. You can do something like this

client.js:

let players = [];

RegisterNetEvent('client-getplayers')
on('client-getplayers', () => {
	let players = GetActivePlayers();
	emitNet("server-updateplayers", players);
});

// this can be done on to update your server.js with players when the have spawned in the server.
on('playerSpawned', () => {
	let players = GetActivePlayers();
	emitNet("server-updateplayers", players);
});

server.js:

RegisterCommand("getplayerstest", function(source, args, rawCommand) {
	var src = source
	emitNet("client-getplayers", src);

}, false);

RegisterServerEvent("server-updateplayers")
onNet("server-updateplayers", async (players) => {
	console.log(JSON.stringify(players))
});

Hope this helps! :smiley:

problem is, there IS no equivalent of
GetPlayers()
which is a server side only function i’m trying to replace.
i tried using the natives @magnesium showed me, however those don’t seem to return anything when called. so i’m stumped. i must just be doing this wrong.
here is my lua function to do this:

for _, v in ipairs(GetPlayers()) do
--do stuff
end

this is effectively what i’m trying to do, SERVER side only. in JS.

i’m mostly just trying to replace it in js without having to hoop jump.
it’s no biggy, i’ll keep fiddling! thanks for the advice!

1 Like

What natives are you using in lua to get it to work?

Well this seems to work:

function GetPlayers() {
	var num = GetNumPlayerIndices()
	var players = [];
	for (i = 0; i < num; i++) {
		players.push( { id: num, identifier: GetPlayerIdentifier(num), name: GetPlayerName(num) } );
	}
	console.log(JSON.stringify(players))
	return players
}

Gets the id, identifier, and playername

It literally is a loop through GetNumPlayerIndices that’s in the HLL for compatibility. There’s nothing special about it that prevents you from implementing it yourself.

thanks for all the patience guys, JS is NOT a language i’m fluent in, just kinda starting to fiddle with, so these questions might seem noob, but my background isn’t in code. i’m just hobby level.
( QUITE different way of having to do things than the lua, and WAY less documentation )

1 Like