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
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.
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