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 number with 'PlayerStage'?

I have asked this question before, but they weren't helpful and nobody is giving the answer.

My script errors in line 31, "ServerScriptService.Stats:31: attempt to index number with 'PlayerStage'".

Script:

local DataStoreService = game:GetService("DataStoreService")
local StageDataStore = DataStoreService:GetDataStore("StageDataStore")
local debounce = false

game:GetService("Players").PlayerAdded:Connect(function(player)
	local players = game:GetService("Players"):GetPlayers()
	
	for i, _ in pairs(players) do
		if i > 1 then
			player:Kick("There is more than 1 player in the server. Rejoin the game!")
			return nil
		end
	end
	
	local Stage = Instance.new("IntValue", player)
	Stage.Name = "Stage"
	
	player.RespawnLocation = game:GetService("Workspace"):WaitForChild("Start").Checkpoint["0"]
	
	local playerUserId = "Player_"..player.UserId
	
	-- Load Data
	
	local data
	local success, errormessage = pcall(function()
		data = StageDataStore:GetAsync(playerUserId)
	end)
	
	if success then
		-- Set data to stage
		Stage.Value = data.PlayerStage
		if data.PlayerStage then
			game:GetService("ReplicatedStorage").StageChanged:FireClient(player, Stage.Value)
		end
		
		for i, v in pairs(data.PlayerObbies) do
			game:GetService("ServerStorage").PassedStage:Fire(i)
		end
	end
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	local PlayerStages = {}
	
	for _, Obby in pairs(game:GetService("Workspace"):GetChildren()) do
		if Obby.Name == "Obby1" or Obby.Name == "Obby2" then
			for __, SpawnLocation in pairs(Obby.Checkpoint:GetChildren()) do
				if SpawnLocation:IsA("SpawnLocation") then
					table.insert(PlayerStages, tonumber(SpawnLocation.Name), Obby)
				end
			end
		end
	end
	
	local data = {
		PlayerStage = player.Stage.Value;
		PlayerObbies = PlayerStages
	}
	
	local success, errormessage = pcall(function()
		StageDataStore:SetAsync(playerUserId, data)
	end)
	
	if success then
		print("Data successfully saved!")
	else
		warn("Error when saving data!")
		warn(errormessage)
	end
end)

3 answers

I just experienced few flaws in your data table (when the player was leaving). Also, there is no BindToClose function. Here is an example for using BindToClose :

local RunService = game:GetService("RunService")

game:BindToClose(function()
	if RunService:IsStudio() then return end -- It is to prevent this function from firing in studio, as it may lead PlayerRemoving and BindToClose both fire in studio and it results in data storing queue.
	for _, players in ipairs(game.Players:GetPlayers()) do -- Creating a for loop
		Save() -- Just an eg
	end
end)

Basically, BindToClose delays the game shut down so that the datastores have enough time time to save the player's data.

To check the data table, just add this line in Line 30:

for i, v in pairs(data) do
	print(i, v)
end

I have already told you before, that the error is trying to say that 'data' is a 'number'. Try using above measures to identify the errors and lemme know.

EDIT : If the above didn't work, try adding an if statement to check whether the data really exists.

3 comments

0

which line do i put this code in?

0

plus, what does save() do?

0

Put this BindToCLose at the end, save() is just a nil function taken as an eg. Just put ur saving code instead of where Save() is there.

You set PlayerStage to player.Stage.Value after you told the server that the stage value equals to it

maybe, your stage data stores a string or else so you need to look at it. You can fix it by changing the datastore name but it will reset your data before

1 comment

0

player.Stage is an IntValue, which can not be a string sooo that can't be the case

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