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!
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.
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.
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.
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.
-- 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)
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.
-- 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
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.