Still have questions? Join our Discord server and get real time help.
Log in to vote

Attempt to index a nil value? (RemoteFunction)

Asked by 2 years ago

Any time that I attempt to use the remote function, I get an error saying that I am attempting to call a nil value on line 9 in my local script, but all values are loaded into the game already. Any help is appreciated.


repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character

CoinGUI = script.Parent.Coins
GemGUI = script.Parent.Gems

GetStatsInvoke = game:GetService("ReplicatedStorage"):WaitForChild("GetStatsInvoke")

    CoinGUI:FindFirstChild("CoinAmount").Text = (tostring(GetStatsInvoke:InvokeServer("Coins").Value).." Coins")
    GemGUI:FindFirstChild("GemAmount").Text = (tostring(GetStatsInvoke:InvokeServer("Gems").Value).." Gems")


GetStatsInvoke = game:GetService("ReplicatedStorage"):WaitForChild("GetStatsInvoke")
StatsFolder = game:GetService("ServerStorage"):WaitForChild("PlayerStats")

GetStatsInvoke.OnServerInvoke = function(plr, Stat) 
    local plrFolder = StatsFolder:WaitForChild(plr.Name)

    if plrFolder:WaitForChild(Stat) ~= nil then
    local Return = plrFolder:WaitForChild(Stat)
    return Return
I'm confused as to what you're doing with "Stat" is it an IntValue? If so, then return the value instead of the IntValue itself. And your return(on the server) should return something if the If statement were to fail. MooMooThalahlah 421 — 2y

1 answer

Log in to vote
Answered by
Scubadoo2 115
2 years ago

I think the main problem is when you set up CoinGUI and GemGUI. Just because the script is loaded does not mean all the other objects loaded, so when it tries to find "Coin", it hasn't been created yet, making the variable nil. Try changing lines 3 and 4 to this:

CoinGUI = script.Parent:WaitForChild("Coins")
GemGUI = script.Parent:WaitForChild("Gems")

Answer this question