How to get the same random values?

This may sound contradictory but bear with me.

Basically, a player shoots an automatic gun and it sprays 5 bullets with a random spread. It’s first done locally for instant feedback of bullets, but the server needs to raycast the values.

Is there anywhere the random values can be synced with the server and client?

If the client sends their version of the random, I’m sure they can change the values, and if the server creates their own random sequence and sends it back to the client, I think that will ruin the instant feedback of the bullets creating.

Use Random.new() and supply a seed. This creates a pseudorandom generator that supplies the same sequence of uniformly random values (given that the seed is the same on the client and server).

2 Likes

Make a script that will create variables in ReplicatedStorage every time a player joins/leaves then set the player’s unique variable to the random number generated

1 Like

I would use a predictive seed that matches on client and server.

Client joins
Server assigns a starting seed to that player
Client uses that starting seed to generate the spread
Client increases the seed by some determined value (1 + 1 = seed is now 2)
Server creates the spread based on the current seed and also increases the seed by the determined number.

Now all you have to do is calculate the shot on the server for whatever shot they are on, since it’s shot gets incremented.

1 Like

Or, for an easier version of this, create two variables in ReplicatedStorage, one should be a String, the other an Integer. When the player shoots the gun, make the String their username and make the integer the random number generated. Use Variable.Changed on the Integer to detect when a player shoots

1 Like

This wouldn’t work if the player is changing an IntValue locally. The changes won’t replicate to the server, so the .Changed event would not fire.

Anyways, storing variables inside ReplicatedStorage is redundant. As @DreamWakeStudios suggested, a predictive seed that is incremented is a viable approach. When a player is added, the starting seed can be synced by the server to the client through a remote event and the server can maintain each player’s seed inside a table.

2 Likes