How can I make splayer unique server-side code?

Hi! :smiley:
I figured out that to secure my game I’ll want to do all of the important stuff on the server, but how do I do that in case of player-unique code? I am trying to achieve something like a LocalScript, that is a separate copy from the other players’ LocalScripts, except the script must be run on the server for security reasons. What would be the best solution for that?

For example, a script that saves the items the player collected into their inventory would have to be server-side, but also unique for each player.
Note: I’m not looking for case-specific solutions for the given example, it’s just there as an example

I could not come up with a solution myself, and I searched for them but have not found any.

Note: Using FilteringEnabled

2 Likes

You can use the PlayerAdded event and run whatever code you want inside of there, no?

There are different ways to run code for each player. I wouldn’t necessarily say that you should run everything on the server. Code for user control or code that needs to be immediate for the client should be done locally. Anything that effects the game state in a possibly dramatic way should be either run or verified by server code.

You also talk about using the same code for both the server and client. You should look into Modules if you are not familiar with them. A module can be required by both the server and the client so that they use the same code to do things.

You could iterate through players individually using a loop like this

for _,player in pairs(game:GetService('Players'):GetPlayers()) do
--
end

You can also run code for when a player is added to the game using the PlayerAdded event that @kinkocat suggested.

game:GetService('Players').PlayerAdded:connect(function(player)
--
end)

Greetings! :wave:

There are many ways to handle something like this, and Roblox provides a variety of different methods.

  • You can use the .PlayerAdded event of Players. This event passes an argument which is equal to the new player. This event fires when someone joins the game.

    game:GetService("Players").PlayerAdded:Connect(function(Player) --Player is equal to the new player instance created
        print(Player.Name.." just joined!")
    end
    

    On a side note, you can also use the .CharacterAdded event of the Player variable, which fires every time the players character is added. It also comes with a variable which is the new Character model.

  • If you’d like to iterate through all current players and execute something, you could use the :GetPlayers() method of Players. This method will return an array of players which you can iterate through using an iterative loop. Here’s how that would look:

    local PlayersArray = game:GetService("Players"):GetPlayers() --This will return all the current players in an array. 
    for _, Player in pairs(PlayersArray) do --The player variable is the player object 
        print("Running code for: "..Player.Name)
    end
    

    If you’d like more information on how arrays (also known as tables) work, you can check out this article on the developer hub.

Have a blessed day! :sunny:

1 Like

Thanks for the replies \o/

What I meant was that the code that handles inventories would be server side, but each player would have to own a unique inventory that is also on the server so that they can’t just exploit it and edit the inventory however they want, but modules sound interesting, I’ll look into that

How would I handle events in this case?
For example, if I’d want something to happen that is individual for each player (like adding the object to their inventory) when that specific player’s head is touching something (Using the Touched event), how would I go about that?
Would I put an event listener into the PlayerAdded event listener, or perhaps I would somehow have an event listener that works for all players, or maybe I’d do something different?..

You could use a RemoteEvent for the client to send signals to the server when they want to do something such as pick up an object. That is probably the best course of action here.

Although, to continue with the example you’d like to try, the could would then need to hook up a touch event for when a character is added.

function characterAdded(character)
local head = character:WaitForChild('Head')
head.Touched:connect(function(hit)
end)
end
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAdded:connect(characterAdded)
end

Modules aren’t a good way to do this. I would just create a table for each player, that will hold all of the objects that are in their inventory. Then, as @VitalWinter, you can create a RemoteEvent which will allow the client to send a signal to the server when they pick or drop an object. If you want more security, you can do things like distance checks to make sure players are within range of the object when they pick it up.

Thanks! Now I see.
I also read somewhere that you would usually want to disconnect unwanted connections, but I’ll look into that myself a bit later.
Have a nice everyday :smile: