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

"Attempt to index number with 'Best'"?

Asked by 3 years ago

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.

0
What line? RBLX_Nat 18 — 3y
0
Line 46/47 JoshuaBytesTV 5 — 3y
0
btw, moving my pc, won't be able to respond for a bit JoshuaBytesTV 5 — 3y
0
Maybe because it cannot find Best, try adding WaitForChild xXLegendGamerz16Xx 231 — 3y
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 JoshuaBytesTV 5 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

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.
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. Ziffixture 6913 — 3y
0
Currently on my phone, but the datastore it saves the value. JoshuaBytesTV 5 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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

Answer this question