Global Variable 'game' not working?

You can write your topic however you want, but you need to answer these questions:

  1. I am trying to use the global ‘game’ but it does not work for whatever reason

  2. For some reason it errors: “ServerScriptService.MainScript:18: attempt to index nil with ‘Game’”

  3. I tested the game on roblox itself and roblox studio and both have the same issue

This is not the StarterPlace, the following code is part of a Server Script called ‘MainScript’ inside of ServerScriptService:

local RS = game:GetService("ReplicatedStorage")
local DSS = game:GetService("DataStoreService")
local httpService = game:GetService("HttpService")
local players = game:GetService("Players")

local provinceDataStore = DSS:GetDataStore("ProvinceDS"..209)
local provinceInfo = RS:WaitForChild("provinceInfo")

local function loadData()
	local provinceName = game.Name
	local provinceData
	
	local success, errorMessage = pcall(function()
		provinceData = httpService:JSONDecode(provinceDataStore:GetAsync("Info"))
	end)
	
	provinceInfo:WaitForChild("Owner").Value = provinceData[provinceName]["Owner"]
	provinceInfo:WaitForChild("Attacker").Value = provinceData[provinceName]["Attacker"]
end

loadData()

The line which errors is:

provinceInfo:WaitForChild("Owner").Value = provinceData[provinceName]["Owner"]

The variable is working completely fine, game.Name in your case equals to the string Game(your game name). The issue is that the variable provinceData is nil, so you try to index it with a string(in this case the string Game) and you get the error attempt to index nil.

The reason provinceData is nil is either because the pcall failed or because the player doesn’t have previous data when entering the game(so you must set the default values).

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.