So I made a module, used that to make a table for my server script. Now I was wondering, how would I get the table from the server script to my local script? If so could you please show an example?
Help would be much appreciated, thanks.
So I made a module, used that to make a table for my server script. Now I was wondering, how would I get the table from the server script to my local script? If so could you please show an example?
Help would be much appreciated, thanks.
Most likely with some form of remote/bindable function. You can just pass the table as an argument depending on the the use case. Sorry no code, but just read up on these things and I think you’ll see they solve your issue.
I think you would like Remote Events would help you. These can help you get from Client to server and back, for example if you have a script in server script service and one in startergui:
Local Script:
local remoteevent = game.ReplicatedStorage.RemoteEvent
remoteevent:FireServer(“Hello world!”)
Server:
local remoteevent = game.ReplicatedStorage.RemoteEvent
RemoteEvent.OnServerEvent:Connect(function(playerwhosent, message))
print(message)
end
you can replace the message with your table
Ok, remote events it is!
How would I be able to get a table of players so:
local players = gameModule.playersInRound
and get it to my client script
Well, you need to make server script to have this code:
local remoteevent = wherever you made your remote event, likely in replicated storage
–now we fire the server, with the table
remoteevent:FireClient(players)
Your client should have this:
local remoteevent = wherever you made your remote event, likely in replicated storage
local players = nil
remoteevent.OnClientEven:Connect(function(player, playerst)
players = playerst
end)
And it should work.
You also should make sure you have made your remote event, like what i put in the code, likely in replicatedstorage, so that both client and server can easily access it.
What do you mean by that???
Ok so here’s what I did! So first I got the remote event I created from replicated storage.
local playerEvent = game.ReplicatedStorage:WaitForChild("PlayerEvent")
Next, I fired the client with the players
local players = gameModule.playersInRound
playerEvent:FireClient(players)
Then, I went to my client script and got the remote even once again.
And lastly, I created the function.
playerEvent.OnClientEvent:Connect(function()
end)
But where do I go to get the table???
You need to define this:
playerEvent.OnClientEvent:Connect(function(plr, players) --plr, players is there
end
so the players is the variable I used for the server side correct. So its the variable that stored the players.
so thats why i named it playerst instead of players
My goal here is so I get the table and use it for this:
previous.MouseButton1Click:Connect(function()
playerEvent.OnClientEvent:Connect(function()
end)
local players = playerEvent
print("clicked")
local max = #players
num = num - 1
print(num)
if num < 1 then
num = max
end
if num >= 0 then
print("num is greater than 0")
local player = players[num]
local playerLocation = game.Players:FindFirstChild(player.Name)
print(player.Name)
if playerLocation then
print("player location")
camera.CameraSubject = playerLocation.Character.Humanoid
status.Text = player.Name
end
end
end)
I want to get the table and make the varaiable = to players
Do you get my point?
previous.MouseButton1Click:Connect(function()
local players = nil
playerEvent.OnClientEvent:Connect(function(plr, playerst)
players = playerst
end)
print(“clicked”)
local max = #players
num = num - 1
print(num)
if num < 1 then
num = max
end
if num >= 0 then
print(“num is greater than 0”)
local player = players[num]
local playerLocation = game.Players:FindFirstChild(player.Name)
print(player.Name)
if playerLocation then
print(“player location”)
camera.CameraSubject = playerLocation.Character.Humanoid
status.Text = player.Name
end
end
end)
This might work
players is underlined in blue so should I put everything in the function.
previous.MouseButton1Click:Connect(function()
playerEvent.OnClientEvent:Connect(function(plr, playerst)
local players = playerst
print("clicked")
local max = #players
num = num - 1
print(num)
if num < 1 then
num = max
end
if num >= 0 then
print("num is greater than 0")
local player = players[num]
local playerLocation = game.Players:FindFirstChild(player.Name)
print(player.Name)
if playerLocation then
print("player location")
camera.CameraSubject = playerLocation.Character.Humanoid
status.Text = player.Name
end
end
end)
end)
You can use module scripts lol:
--modulescript
local module = {
playersTable = {}
}
return module
--ServerScript
local mod = require(script.ModuleScript) --// pretend it is in the script
print(mod.playersTable)
--serverscript2
local mod = require(script.ModuleScript)
print(mod.playersTable)
I use this for my Custom Function 2.0
I think that should work. Can you test it? I can’t currently test the code right now.
Not the issue. Why don’t you READ the first post? It will get you the problem.
I am tired rn sorry lol
Just use remoteFunctions or remotevents
YES, but the issue here is how do I do that??? how would I get the table I used in the server script
local players = gameModule.playersInRound
and use it in my local script???
You can use a remote or bindable function to do this.
Here’s an example:
local listOfPlayers = {
'Player1',
'Player2'
}
local bindableEvent = game.ReplicatedStorage.GetPlayers
bindableEvent.OnInvoke = function()
return listOfPlayers
end
local bindableEvent = game.ReplicatedStorage.GetPlayers
local players = bindableEvent:Fire()
print(players) --[[ ->
"Player1",
"Player2"
]]
(Also I’m pretty sure both scripts need to be the same type of script)