I mean the scripts. if you got an error index nil with ‘PlayerGui’
then there was another issue unrelated to the remote itself
here is the two scripts:
--serverscript
local remote = game.ReplicatedStorage.Shop
local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("Info")
local function saveData(player) -- The functions that saves data
local tableToSave = {
player.leaderstats.Gold.Value; -- First value from the table
player.leaderstats.WalkSpeed.Value; -- Second value from the table
remote.FetchMaxAndPrice:FireServer(player)[1];
remote.FetchMaxAndPrice:InvokeClient(player)[2]
}
local success, err = pcall(function()
Data:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
end)
if success then -- If the data has been saved
print("Data has been saved!")
else -- Else if the save failed
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(p)
wait(1)
-- Currency
print(remote.FetchMaxAndPrice:FireClient(p)[1], remote.FetchMaxAndPrice:FireClient(p)[2])
local Leaderstats = Instance.new("Folder", p)
Leaderstats.Name = "leaderstats"
local gold = Instance.new("IntValue", Leaderstats)
local walkspeed = Instance.new("IntValue", Leaderstats)
gold.Name = "Gold"
walkspeed.Name = "WalkSpeed"
-- DataBase
local data
local success, err = pcall(function()
data = Data:GetAsync(p.UserId)
end)
if success then
gold.Value = data[1]
walkspeed.Value = data[2]
remote.ChangePriceAndMax:FireClient(game.Players:FindFirstChild(p.Name),data[3], data[4])
else
print("Couldn't load!")
warn(err)
end
p.CharacterAdded:Connect(function()
p.Character.Humanoid.WalkSpeed = walkspeed.Value+16
p.Character.Humanoid.Died:Connect(function()
p.Team = game.Teams.spectator
end)
walkspeed.Changed:Connect(function()
print("Changed!!")
p.Character.Humanoid.WalkSpeed = walkspeed.Value+16
end)
end)
game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
warn(err)
end
end)
game:BindToClose(function() -- When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
warn(err)
end
end
end)
end)
--localscript
local shop = game.ReplicatedStorage.Shop
local remote = game.ReplicatedStorage.Shop:WaitForChild("FetchMaxAndPrice")
remote.OnClientEvent:Connect(function(player)
local p = player.PlayerGui:WaitForChild("ScreenGui").Speed
return {p.max.Value, p.speedprice.Value}
end)
i have to go now, i will be online, maybe from 5:00pm, to 6:00pm (AST).
These can’t be used interchangeably. Also, when you’re in a localscript, you don’t (and shouldnt) need to get the player from the remote itself. You can access the current player with Players.LocalPlayer
given that Players
is game:GetService("Players")
or however you choose to get the Players service. When you InvokeClient
(or FireClient
for that matter), the first argument is always the player to send the remote to. This is not the first argument passed into the function/event. Therefore, if you wanted to do it the way it looks like you want to do it (which I personally wouldn’t) you’d have to InvokeClient(player, player)
. But you really should never get values like this from the client. Nor should you InvokeClient
of the same event more than once if you just need the same data.
Meaning instead of
local x = InvokeClient(player)[1]
local y = InvokeClinet(player)[2]
You should
local data = InvokeClient(player)
local x = data[1]
local y = data[2]
Again, and I’ve said this a lot because I really want to emphasize it. You should not get data like this from the client unless you want the client to have full control over the value which allows exploitation of the value.
the code works well, but idk if this is because the function, but it returns nil
local shop = game.ReplicatedStorage.Shop
local remote = game.ReplicatedStorage.Shop:WaitForChild("FetchMaxAndPrice")
function func()
local p = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui").shop.Speed
return {p.max.Value, p.speedprice.Value}
end
remote.OnClientInvoke = func
--[[
output = nil nil
]]--