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 'Best'"?

So - I'm working on a remake of Super Check Point, and i need bests that save. Here's what i have so far

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local db = 0

local function UpdateStats(plr, stage, mode, dataa, datab)
	local data = playerData:GetAsync(plr.UserId)
	local best = plr:WaitForChild("Best")
	local prbest = plr:WaitForChild("PRBest")
	local plrdata = ("Player_" .. plr.UserId)
	if mode == "Standard Run" then
		if dataa < stage then
			best.Value = stage
			local success, err = pcall(function()
				playerData:SetAsync(plrdata, best.Value)
			end)
			if not success then
				warn('Could not save data!')
			end
		end
	else 
		if datab < stage then
			prbest.Value = stage
			local success, err = pcall(function()
				playerData:SetAsync(plrdata, prbest.Value)
			end)
			if not success then
				warn('Could not save data!')
			end
		end
	end
end


local function onPlayerJoin(plr)  -- Runs when players join
	local leaderstats = plr:WaitForChild("leaderstats")
	local best = Instance.new("NumberValue")
	best.Parent = plr
	best.Name = "Best"
	local prbest = Instance.new("NumberValue")
	prbest.Parent = plr
	prbest.Name = "PRBest"
	local playerUserId = "Player_" .. plr.UserId
	local data = playerData:GetAsync(playerUserId)
	print(playerUserId)
	if data then
		best.Value = data.Best --This is the line returning the error
		prbest.Value = data.PRBest
	else
		best.Value = 1
		prbest.Value = 1
	end
	plr.leaderstats.Stage.Changed:Connect(function()
		if db == 0 then
			db = 1
			UpdateStats(plr,plr.leaderstats.Stage.Value,plr.leaderstats.Mode.Value, best.Value, prbest.Value)
			wait(0.2)
			db = 0
		end
	end)
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

but when I run it, it returns the error "Attempt to index number with 'Best'". Can I get some help here? I don't know why its doing this.

5 comments

0

What line?

0

Line 46/47

0

btw, moving my pc, won't be able to respond for a bit

0

Maybe because it cannot find Best, try adding WaitForChild

0

Legend, it makes a new value in the player. It needs to MAKE IT before it gets the stuff. and also, this is about the fact it thinks that data = 2

2 answers

You can't set a Value as Object. Instead of using best.Value = data.Best, you have to get the Value of Best, not the object. Use instead:

best.Value = data.Best.Value -- .Value gets the Value of the Object.

2 comments

0

You do realize that that's not the problem right? The error itself already hints at what's really wrong: they're saving the 'Best' value already, 'data' already correlates to the integer, which is why trying to do `data.Best' is raising an indexing error.

0

Currently on my phone, but the datastore it saves the value.

This question has been fixed by my self - I had to make a few changes. Too lazy to type them here. maybe later.

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