ServerScriptService.Script:36: attempt to index number with 'CoinsAmount' (Roblox Studio)

50 Views Asked by At

So I am making data store for a simulator type game on roblox studio using luau and got this error which i cant crack.

Heres the code

local DataStoreService = game:GetService("DataStoreService")
local CoinsStore = DataStoreService:GetDataStore("CoinsStore")

game.Players.PlayerAdded:Connect(function(player)
    local stats = Instance.new("Folder", player)
    stats.Name = "Stats"

    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

    local Coins = Instance.new("IntValue", leaderstats)
    Coins.Name = "Coins"

    local Rebirths = Instance.new("IntValue", leaderstats)
    Rebirths.Name = "Rebirths"

    local StomachMaxSpace = Instance.new("IntValue", stats)
    StomachMaxSpace.Name = "StomachMaxSpace"

    local Stomach = Instance.new("IntValue", stats)
    Stomach.Name = "Stomach"
    
    local UID = player.UserId
    
    --Load Data
    
    local data
    
    
    local success, errormessage = pcall(function()
        
         data = CoinsStore:GetAsync(UID)
    end)
    
    if success then
        Coins.Value = data.CoinsAmount
        StomachMaxSpace.Value = data.StomachCapacity
        Stomach.Value = data.Stomach
    end
    
        
    
end)   

--save data

game.Players.PlayerRemoving:Connect(function(player)
    local UID = player.UserId
    
    local data = {
        CoinsAmount = player.leaderstats.Coins.Value,
        StomachCapacity = player.Stats.StomachMaxSpace.Value,
        Stomach = player.Stats.Stomach.Value
    }   
    
    local success, errormessage = pcall(function()
        CoinsStore:SetAsync(UID, data)
    end)
    
    if success then
        print("All Data Saved")
    end
    
    
    
end)

Please Help me solve the error because I need to get this game out

Error: ServerScriptService.Script:36: attempt to index number with 'CoinsAmount'

1

There are 1 best solutions below

3
jwielink On

I believe CoinsStore:GetAsync(UID) is returning a number, if this function is returning an error code, then you might want to check the return value of the function as well before continuing.