How can I call a Table from a Script into a LocalScript

Hello! I am wondering how I would be able to call a Table from a Script inside ServerScriptService, into a LocalScript inside PlayerGui.

Let’s say you have a table inside a Script, and it would be set up like this

-- Script
local u2 = {5, 893, 328, 281}

And then you have a local script that wants to retreat from the script, let’s say

-- Local Script
scirpt.Parent.Text = u2[2]

How would I be able to do something like that? You call a function or event, and the ServerScriptService Script will give the LocalScript the result to u2[2], if that makes any sense.

If this is possible, please let me know! Any help will be very much appreciated! :smile:

1 Like

You can use ModuleScripts , they are useful for such cases

1 Like

(I didn’t mean to edit the message, I have no idea what I wrote)

1 Like

You can use remote events and pass it from the server to the client.

1 Like

But how am I supposed to give the Client the table number through a result? An example will be greatly appreciated.

–Client

local RsService = game:GetService("ReplicatedStorage")
local module = require(RsService.ModuleScript)


print(module[1]) --> 5

–Module script inside ReplicatedStorage

local tab = {5,893,328,281}

return tab

1 Like

Yes, I know how ModuleScripts work, but I was asking because the value is changing every time a player purchases something, so I am pretty sure a ModuleScript wouldn’t work in my instance. Sorry if I didn’t explain that in the main message.

1 Like

Once you pass the table through the remote you can index it as you would in the server. Just maybe a different variable holding the table but that’s up to you to set. See DevHub’s reference on remote events for more details. It’s just like passing anything else by a remote just that your passing a table.

1 Like

–Server

local table = {5,2,1}
local event = game.ReplicatedStorage.Remote


task.wait(3)
event:FireAllClients(table)

–Client

local RsService = game:GetService("ReplicatedStorage")
local event = RsService:WaitForChild("Remote")


event.OnClientEvent:Connect(function(item)
	print(item[1]) --> 5
end)
2 Likes

I only want it to go to one client though? This is what I have for the script

-- ServerScriptService Script
local ds = game:GetService("DataStoreService"):GetDataStore("UpgradeTableTest8348")
local gameFolder = game.ReplicatedStorage:FindFirstChild("Game")
local remoteFunctions = gameFolder:FindFirstChild("RemoteFunctions")
local remoteEvents = gameFolder:FindFirstChild("RemoteEvents")

local u1 = {}
local l_UpgradeModule_1 = require(game.ReplicatedStorage:WaitForChild("Framework"):WaitForChild("Modules"):FindFirstChild("UpgradeDataModule"))


game.Players.PlayerAdded:Connect(function(player)
	local stats = ds:GetAsync(player.UserId)
	if stats ~= nil then
		table.insert(u1, stats[1])
		table.insert(u1, stats[2])
		print(u1)
	else
		table.insert(u1, l_UpgradeModule_1["Player Walkspeed"]["prices"][1])
		table.insert(u1, l_UpgradeModule_1["Player Walkspeed"]["minUpgrade"])
		print(u1)
	end
	player.CharacterAdded:Connect(function(char)
		wait()
		char.Humanoid.WalkSpeed = 16 + (u1[6] * 3)
		if u1[2] == 0 then 
			u1[1] = l_UpgradeModule_1["Player Walkspeed"]["prices"][1]
		end
	end)
end)
game.Players.PlayerRemoving:Connect(function(player)
	ds:SetAsync(player.UserId, u1)
	print(u1)
end)

remoteFunctions:FindFirstChild("UpgradeSpeed").OnServerInvoke = function(player, price, maxSlot)
	if player.leaderstats.Gems.Value >= price and u1[6] < maxSlot then 
		player.Character.Humanoid.WalkSpeed +=3
		player.leaderstats.Gems.Value -=price
		print(u1)
		u1[5] *= 1.5
		u1[6] += 1
		print(u1)
		return 'Bought'
	else
		return 'Error'
	end
end

And then this is the localscript

-- Local Script
local players = game:GetService("Players")
local player = players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local TweenService = game:GetService("TweenService")
local replicatedStorage = game.ReplicatedStorage

local remoteFunctions = game.ReplicatedStorage.Game:FindFirstChild("RemoteFunctions")
local remoteEvents = game.ReplicatedStorage.Game:FindFirstChild("RemoteEvents")
local holder = playerGui:WaitForChild("UpgradeGUI")
local mainPC = playerGui:WaitForChild("Main PC")
local container = holder:FindFirstChild("Frame"):WaitForChild("UpgradesScrolling"):FindFirstChild("Container")
local sfx = replicatedStorage:FindFirstChild("SFX")

local framework = game.ReplicatedStorage:FindFirstChild("Framework")
local modules = framework:FindFirstChild("Modules")
local v2 = require(modules:WaitForChild("UpgradeDataModule"))
local debounce = false


if player.Upgrades.SpeedUpgrade.Value == v2["Player Walkspeed"]["maxUpgrade"] then
	container.Speed.PriceText.Text = 'Maxed'
	container.Speed.PriceText.RainbowScript.Disabled = false
end

container.Speed.UpgradeBTN.MouseButton1Click:Connect(function()
	if not debounce then
		debounce = true
	local result = remoteFunctions:WaitForChild("UpgradeSpeed"):InvokeServer(player.Upgrades.SpeedUpgradePrice.Value,v2["Player Walkspeed"]["maxUpgrade"])
		task.wait(.2)
		debounce = false
	if result == 'Bought' then
		sfx.PurchaseSuccess:Play()
		tweenPrice()
	else
		sfx.CodeSound.Error:Play()
			if player.Upgrades.SpeedUpgrade.Value == v2["Player Walkspeed"]["maxUpgrade"] then
				container.Speed.PriceText.Text = 'Maxed'
				container.Speed.PriceText.RainbowScript.Disabled = false
			end
		end
	end
end)

function tweenPrice(p1)
	
end

I only want it to go to that one client that pressed the button, if that makes any sense.

1 Like

Pass the player using FireClient instead of FireAllClients, do this in your player added function.

How would I be able to do this a remotefunction?

1 Like

Okay I see, in the function that handles the button pressing you have to fire to the server. Put a OnClientEvent in the server that upon receiving the signal, it fires back to the client the table.

Just fire the player reference when your clicking the button so it has the player reference to return the signal back to.

So whenever I try and just do the basic thing, it gives this error.

-- Script
local table2 = {3,4,3}
task.wait(5)
remoteEvents.UpgradePrice:FireClient(table2)
-- Local Script
local remoteEvents = game.ReplicatedStorage.Game:FindFirstChild("RemoteEvents")

remoteEvents.UpgradePrice.OnClientEvent:Connect(function(item)
	print(item[1])
end)
1 Like

You need to include which player needs to receive the event, so your first argument in remoteEvents.UpgradePrice:FireClient(player, table2) should be that player.

1 Like

I just did a simple thing which it’ll do this

-- Script
remoteFunctions:FindFirstChild("TEST").OnServerInvoke = function(player)
	if player.leaderstats.Gems.Value >= 5 then
		remoteEvents.UpgradePrice:FireClient(player, table2)
		return 'test'
	end
end
-- Local Script
script.Parent.TextButton.MouseButton1Click:Connect(function()
	local yes = remoteFunctions:WaitForChild("TEST"):InvokeServer()
	
	if yes == 'test' then
		remoteEvents.UpgradePrice.OnClientEvent:Connect(function(item)
			print(item[1])
		end)
	end
end)

But everytime I click, it’ll print it once, then it’ll print 2, 4, etcetera
image

You’re creating a new connection to the remove event every time you click the button. You either connect to the remote event outside of the function or you disconnect the event after you received a response from the server.

Besides that, I don’t get why you are trying to pass a value to the same script via a remote event if you’re already using a remote function.

1 Like