How would I create individual functions for each player server side?

I need to be able to have the same functions for every player, but so that every function is separate from the others. Similar to how it would work if you put a function in a local script, and it duplicated the local script for each player.

This needs to be done on the server for game security. How do I do this?

Why does it have to be done on server? Why cant you just put it in StarerPlayer/StarterPlayerScripts

Game security.

1 Like

If you are worried people will cheat, nothing affecting the gameplay loop should be handled on the client, the client should pass input as events to scripts on the server within say CharacterScripts

So your suggesting just placing a normal script in a location that will duplicate it for every player?

StarePlayerScripts are for local scripts that every player has, and StarterCharacterScripts would be placed within every ingame character on the server, but would be destroyed and recreated on death

Is there any place besides StarterCharacterScripts I could put it? I would like it to run regardless of the state of the character.

ServerScriptService is the suggested place. Scripts within get run automatically and are not replicated to the client.

But not created for each player?

You do not need one for each player. Pass the player as a parameter in teh event, let the script in SSS handle pointing the logic at the given player’s character.

That’s pretty much what I am doing right now, and it isn’t working. There is probably some solution to my current method, but it’s a lot of code, and I have been hesitant to ask for a review because of that.

What isnt working?

I am using the player as the parameter in a PlayerAdded event. All functions within the event are only affecting the last player to join. My friend who’s been helping me a lot claims that a “new event is created for each player” each time it is called, but I don’t think this is the case.


Consider having only one event per concept, and checking the passed player as an arguement.
In mine I have an event for requesting an action, and an event for a reply to that action.

I don’t really understand what you mean. I don’t think I was clear when I said its creates a new event. What I meant was, my friend claims that each time the event is fired, the function itself “duplicates” for each player, running all the code in the event function for everyone that joins.

( Example of what would actually get “duplicated”, not the event itself. )

He is correct. This gets run for every playeradded, and if its in a local scirpt it will happen N times where N is the number of players in game every time a player is added.

So for four players it would run 10 times i think IF THIS IS IN STARTER PLAYER/CHARACTER

This is in a regular script in the workspace.

Then it would happen once per player when they load in.

This will create a function for every player:

local playerFuncs = {}

local function create(player)
    playerFuncs[player] = function()
        -- do something or require a module
    end
end

local Players = game:GetService'Players'
Players.PlayerAdded:Connect(create)
for i, player in ipairs(Players:GetPlayers()) do
    create(player)
end
5 Likes