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

Attempt to index a nil value? (RemoteFunction)

Asked by 6 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.

Local:

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")

game.Players.LocalPlayer:WaitForChild("InGame").Changed:Connect(function()
    CoinGUI:FindFirstChild("CoinAmount").Text = (tostring(GetStatsInvoke:InvokeServer("Coins").Value).." Coins")
    GemGUI:FindFirstChild("GemAmount").Text = (tostring(GetStatsInvoke:InvokeServer("Gems").Value).." Gems")
end)

Server:

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
    end
end
0
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 — 6y

1 answer

Log in to vote
0
Answered by
Scubadoo2 115
6 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")
Ad

Answer this question