Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is this Remote Function returning nil?

Asked by 10 years ago

I'm trying to create a DataStore system that saves and loads number values when a player joins or leaves the game. To accomplish this, I clone a model with number values inside of it into ServerStorage when a player joins. Then, I rename the model to (player's name)Data. I'm running into a problem where the remote function is not returning the model, therefore not allowing me to manipulate the values.

First, I start by cloning the 'Data' model already inside of ServerStorage. NOTE: The 'default' table is indexed if the player has never player before. For example, if they haven't played before (no DataStore key is found), set the cash to 50. Otherwise, load the data.

local storage = game.ServerStorage
local service = game:GetService("DataStoreService"):GetDataStore("ForeverDevStats")
local default = {
    cash = 50
}

game.Players.PlayerAdded:connect(function(player)
    local data = storage.Data:clone()
    data.Parent = storage
    data.Name = player.Name .. "Data"
    for _, store in ipairs(data:GetChildren()) do
        --load the saved values, or set to the default value
        store.Value = service:GetAsync(tostring(player.userId) .. store.Name:lower()) or default[store.Name:lower()]
    end
end)

Next, I want to attach each value to a .Changed event (!!in a different script!!), so I can use them accordingly in my game. I do this by retrieving the data model through the use of a remote function.

The remote function is located in a model in Workspace, and is called 'GetPlayerData'. The code is as follows:

function script.Parent.GetPlayerData.OnServerInvoke(player)
    return game.ServerStorage:FindFirstChild(player.Name .. "Data")
end

It is important that I note that the function does in fact find the data model, HOWEVER, it returns nil. Here is the script where I try to attach each value to a .Changed event:

--local script

local screen = script.Parent:WaitForChild("ScreenGui")
local displays = {
    cash = screen:WaitForChild("Cash")
}

--inefficient, but I want to make sure the data exists
repeat wait() until Workspace.Functions.GetPlayerData:InvokeServer()
local data = Workspace.Functions.GetPlayerData:InvokeServer()

--the error is that 'data' is a nil value
for _, storage in ipairs(data:GetChildren()) do
    storage.Changed:connect(function(value)
        --manipulate a gui (not important)
        displays[storage.Name:lower()].Output.Text = displays[storage.Name:lower()].StringOutput.Value:gsub("VALUE", tostring(value))
    end)
end

For some reason, the remote function is returning a nil value instead of the player's data model. How can I fix this? Thanks in advance.

0
Is the data being added to the ServerStorage like it's supposed to? Tkdriverx 514 — 10y
0
Yes. I made sure to check. Everything is being created properly. The only problem is that the remote function is returning nil for some reason. ForeverDev 155 — 10y
0
I think I understand what is happening. Objects in ServerStorage cannot be changed from the client. I think that I have to have the .Changed function inside a remote function. ForeverDev 155 — 10y

1 answer

Log in to vote
2
Answered by
Merely 2122 Moderation Voter Community Moderator
10 years ago

Because none of the objects in ServerStorage replicate to the client. You should put them in ReplicatedStorage if you want them to be accessible to both server and client scripts.

1
I thought that you could still replicate data from ServerStorage through a RemoteFunction that returns the data back to the client. Apparently not. xD Tkdriverx 514 — 10y
1
You can access ServerStorage from a Script and shouls be able to pass data from it to a LocalScript via a normal Script, but that communication still has to be via a LocalScript-accessible proxy. :P adark 5487 — 10y
0
I just created remote functions to get and change values in Server Storage. I'll mark your answer as correct, though. ForeverDev 155 — 10y
Ad

Answer this question