So I am building a controls menu for my game in which the player is able to edit the keys that some actions within the game are binded to (for instance, running or toggling a flashlight.)
So far everything has been going well, except for the data saving part of it.
In accordance with the official documentation of Roblox, I have created the following script:
game.Players.PlayerRemoving:Connect(function(player)
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
-- Data store key and value
local playerFlashlightButton = player:WaitForChild("PlayerGui").ControlMenu.FlashlightButton.Text
local playerRunningButton = player.PlayerGui.ControlMenu.RunButton.Text
-- Set data store key
local setSuccess, errorMessage = pcall(function()
goldStore:SetAsync(tostring(player.UserId), playerFlashlightButton, playerRunningButton)
end)
if not setSuccess then
warn(errorMessage)
end
end)
game.Players.PlayerAdded:Connect(function(player)
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
-- Get the data
local getSuccess, currentGold = pcall(function()
return goldStore:GetAsync(tostring(player.UserId))
end)
if getSuccess then
print(currentGold)
-- Print the data
end
end)
The problem is that one of the very last lines of the code, the one where the data is printed, returns “nil” in output. I have already tried swapping out “currentGold” for “goldStore,” but it has just returned the same result. If anyone here knows how to solve this problem, please tell me. Thank you.