Hi, I was wondering if I could load string values when the player is added
My current script:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("KeybindsStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local keybinds = ReplicatedStorage:WaitForChild("Keybinds")
local function create_table()
local player_stats = {}
for _, stat in pairs(keybinds.Shoot:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player)
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
DataStore:SetAsync(playerUserId, player_stats)
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerRemoving:Connect(onPlayerExit)
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("KeybindsStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local keybinds = ReplicatedStorage:WaitForChild("Keybinds")
local function onPlayerAdded(player)
local data
local success , err = pcall(function()
data = DataStore:GetAsync("Player_" .. player.UserId)
end)
if success then
warn('Loaded data for player: ' .. player.Name)
elseif not success then
warn('Loading data for player: ' .. player.Name .. " has failed!")
end
end
local function create_table()
local player_stats = {}
for _, stat in pairs(keybinds.Shoot:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player)
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
DataStore:SetAsync(playerUserId, player_stats)
end)
if success then
warn('Saved data for player: ' .. player.Name)
elseif not success then
warn('Saving data for player: ' .. player.Name .. " has failed!")
end
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:Connect(onPlayerExit)
But when I change one of the string values, it successfully saves but doesn’t load the players data