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

DataStoreService Error : Attempt to index local 'GetCoins' (a number value) ?

Asked by 4 years ago

I've made (with the help of some videos) a DataStore script, and it was supposed to work but it didn't. Here's the script :

local DSService = game:GetService("DataStoreService")
local playerCoins = DSService:GetDataStore("plrCoins")

game.Players.PlayerAdded:connect(function(player)
     local playerId = "player_"..player.userId
     local coins = Instance.new("IntValue", game.Workspace.Stats) -- the value is stored inside a Folder in game.Workspace
     local GetCoins = playerCoins:GetAsync(playerId) -- is supposed to get the value stored in the DataStoreService

     coins.Value = GetCoins[playerId] or 0 -- Error at this line. Should set coins' value to the one stored (or to 0 if nothing is found)

     playerCoins:SetAsync(playerId, coins.Value) -- Update the value of coins inside the DataStoreService
end)

The error is : "ServerScriptService.Script:9:Attempt to index local 'GetCoins' (a number value)"

I think it has something to do with tables but i'm not sure, i didn't really understand, so could someone explain me please ? Just to mention, sometimes, it works and some other times it just shows this error when running the game on Roblox Studio

0
Put the folder inside the player and keep it in a local script. PrismaticFruits 842 — 4y

2 answers

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

So, your error here "Attempt to index local 'GetCoins' (a number value)" is happening because GetCoins is your saved data, so by putting [playerId] after it, your trying to access the playerId as if it was inside your saved value, but it's not.

All you need to do to fix this, is to remove the [playerId] when getting data.

I removed the [playerId] and in case you don't have it, I added a save function for when the player leaves.

local DSService = game:GetService("DataStoreService")
local playerCoins = DSService:GetDataStore("plrCoins")

game.Players.PlayerAdded:connect(function(player)
    local playerId = "player_"..player.userId
    local coins = Instance.new("IntValue", game.Workspace.Stats)
    local GetCoins = playerCoins:GetAsync(playerId)

    coins.Value = GetCoins or 0

    playerCoins:SetAsync(playerId, coins.Value)
end)

game.Players.PlayerRemoving:Connect(function(player)
    local playerId = "player_"..player.userId
    local coins = game.Workspace.Stats.Value

    playerCoins:SetAsync(playerId, coins.Value)
end)

game:BindToClose(function()
    wait(5)
end)

Edit: The game:BindToClose(function() is there to keep the server from closing before the DataStore has a chance to save.

0
It perfectly worked ! Thank you so much ! NotZuraax 68 — 4y
0
Np, glad to help. kkkeelan999 92 — 4y
Ad
Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
4 years ago

The error message tells you to need the GetCoins was a number value, that counts of nil. To patch this, do replace this in line 9,

coins.Value = GetCoins[tostring(playerId)] or 0

I don't know does this work or not, because I didn't tested. Comment below if you have more questions, or the answer not working.

Also, if this helped you, mark this as the correct answer, thanks for your cooperation :D

0
not exactly how "or" works DeceptiveCaster 3761 — 4y
0
Didn't work, there's still the same error message, with not changement NotZuraax 68 — 4y
0
@BashCaster That's a valid use of or. If the table index is truthy, that value will be returned. If it's not, 0 will be returned. This is essentially a ternary operation. ScriptGuider 5640 — 4y

Answer this question