Is there anything wrong with my data store to cause this?
game.ReplicatedStorage:WaitForChild("PlrJoined").OnServerEvent:Connect(function(player, statistics, ID)
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PLRstrength")
local playerUserID = ID
local PLRstrength = statistics
local setSuccess, errorMessage = pcall(function()
goldStore:SetAsync(playerUserID, PLRstrength)
end)
if not setSuccess then
warn(errorMessage)
end
-- Read data store key
local getSuccess, currentGold = pcall(function()
return goldStore:GetAsync(playerUserID)
end)
if getSuccess then
player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value = PLRstrength
end
end)
Heres the problem, something like this wouldent work because it still needs the first few local’s so what do I do?
game.ReplicatedStorage:WaitForChild("PlrLeft").OnServerEvent:Connect(function(player, statistics, ID)
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PLRstrength")
local playerUserID = ID
local PLRstrength = statistics
local setSuccess, errorMessage = pcall(function()
goldStore:SetAsync(playerUserID, PLRstrength)
end)
if not setSuccess then
warn(errorMessage)
end
end)
game.ReplicatedStorage:WaitForChild("PlrJoined").OnServerEvent:Connect(function(player, statistics, ID)
local getSuccess, currentGold = pcall(function()
return goldStore:GetAsync(playerUserID)
end)
if getSuccess then
player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value = PLRstrength
end
end)
You would be able to do something like that, but you should also make your statistics variable more accessible for your script, that way you would be able to run the standard roblox player functions. (instead of using server/client events)
game:BindToClose(function()
for i,p in pair game.player:GetPlayers() do
task.Spawn(function()
local setSuccess, errorMessage = pcall(function()
goldStore:SetAsync(playerUserID, PLRstrength)
end)
end
end
end)
I followed all the suggestions but it still doesn’t work:
game.ReplicatedStorage:WaitForChild("PlrJoined").OnServerEvent:Connect(function(player, statistics, ID)
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PLRstrength")
local playerUserID = ID
local PLRstrength = statistics
game.Players.PlayerRemoving:Connect(function(plr)
local playerUserID = ID
local PLRstrength = player.Character.StatsLocal.LocalPlayerStrength.Value
local setSuccess, errorMessage = pcall(function()
goldStore:SetAsync(playerUserID, PLRstrength)
end)
if not setSuccess then
warn(errorMessage)
end
end)
-- Read data store key
local getSuccess, currentGold = pcall(function()
return goldStore:GetAsync(playerUserID)
end)
if getSuccess then
player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value = PLRstrength
end
end)
game.ReplicatedStorage:WaitForChild(“PlrJoined”).OnServerEvent:Connect(function(player, statistics, ID)
maybe u can just do players.playeradded instead of firing a remote when a player joins
the problem is that the value deletes itself as soon as I leave so it doesn’t save
game.Players.PlayerAdded:Connect(function(player)
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PLRstrength")
local playerUserID = player.UserId
local PLRstrength = player.Character.StatsLocal.LocalPlayerStrength.Value
game.Players.PlayerRemoving:Connect(function(plr)
local playerUserID = player.UserId
local PLRstrength = player.Character.StatsLocal.LocalPlayerStrength.Value
local setSuccess, errorMessage = pcall(function()
goldStore:SetAsync(playerUserID, PLRstrength)
end)
if not setSuccess then
warn(errorMessage)
end
end)
-- Read data store key
local getSuccess, currentGold = pcall(function()
return goldStore:GetAsync(playerUserID)
end)
if getSuccess then
player.Character:WaitForChild("StatsLocal").LocalPlayerStrength.Value = PLRstrength
end
end)