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

"attempt to index a number value" DataStore?

Asked by 6 years ago
local DataStoreService=game:GetService("DataStoreService")
local DataStore=DataStoreService:GetDataStore("Player_Data")
game.Players.PlayerAdded:connect(function(Player)

    local gameData;     
    local Stats=Instance.new('Folder')
    Stats.Name="plrData"
    local Money=Instance.new('NumberValue',Stats)
    Money.Name="Credits"
    Stats.Parent=Player
    local SpecialMoney=Instance.new('NumberValue',Stats)
    SpecialMoney.Name = "Resort Tokens"
    local ownedWeapons=Instance.new('Folder',Stats)
    ownedWeapons.Name = "ownedWeapons"




    local CanSave=Instance.new('BoolValue',Player) 

    CanSave.Name="CanSaveData"
    CanSave.Value=true

    local DataFetchSuccess,ErrorMessage=pcall(function()  
    PlayerData=DataStore:GetAsync(tostring(Player.UserId))
    end)

    if DataFetchSuccess then 

        if PlayerData~=nil then 
            PlayerData:Clone().Parent = Player
        else 
            Player.plrData.Credits.Value=100    
        end

    else 


        Player.CanSaveData.Value=false
        Player:Kick("[SERVER] DS002 (Check the Manual for more information.)")  

    end

end)
game.Players.PlayerRemoving:connect(function(Player)

    if Player.CanSaveData.Value==false then return end 

    local PlayerData=Player.plrData

    local DataWriteSuccess,ErrorMessage=pcall(function() 
        DataStore:SetAsync(tostring(Player.UserId),PlayerData)
    end)    

    if not DataWriteSuccess then 

        local Retry_Count=0

        while Retry_Count<6 do
            wait(60) 
            local Succeded,Error=pcall(function()
                DataStore:SetAsync(tostring(Player.UserId),PlayerData)
            end)
            if Succeded then break end 
            Retry_Count=Retry_Count+1
        end

    end

end)

On Line 31, it gives me the title error. How could I clone the player's data if they played already?

1 answer

Log in to vote
0
Answered by
gullet 471 Moderation Voter
6 years ago

As the error said you're trying to index a number. PlayerData=DataStore:GetAsync(tostring(Player.UserId) returns a number, not a NumberValue object. You can't call Clone on a number. Perhaps you meant to do Player.plrData.Credits.Value = PlayerData

0
How do I clone the folder from the DataStore to the player? ShutokouBattle 227 — 6y
0
The DataStore didn't contain a folder, if you have a folder then you can use Clone. But in this case you had a number. You can't save objects like folders in DataStore. gullet 471 — 6y
0
Well how could I save plrData? ShutokouBattle 227 — 6y
0
Save it as something else and not a folder, a table perhaps or a key for each value. gullet 471 — 6y
View all comments (2 more)
0
I want to use a table. How do I use tables with DataStores? (sorry for excessive asking xd) ShutokouBattle 227 — 6y
0
You just use them... Try having a look at the wiki for datastore and table if you're unfamiliar with them. gullet 471 — 6y
Ad

Answer this question