Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Attempt to index a nil value? (RemoteFunction)

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

1 comment

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.

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.