I've been working on a basic simulator game, and was wondering what was wrong with my script, also i'm trying to make it save multiple data values:
Heres the first script:
local ds = game:GetService("DataStoreService")
local statsStore = ds:GetGlobalDataStore("PlayerStats")
function getPlayerData(plr)
local success, data = false, nil
repeat
success = pcall(function()
data = statsStore:GetAsync("Data"..plr.UserId)
end)
wait(2)
until success
plr.DataLoaded.Value = true
return data
end
function savePlayerData(plr)
if plr.DataLoaded.Value then
statsStore:SetAsync("Data"..plr.UserId, plr.leaderstats.Lifts.Value, plr.Forky.Value)
statsStore:SetAsync("Data"..plr.UserId, plr.Forky.Value)
end
end
function givestats(plr)
local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"
local lifts = Instance.new("IntValue", leaderstats)
lifts.Name = "Lifts"
local loaded = Instance.new("BoolValue", plr)
loaded.Name = "DataLoaded"
local Fork = Instance.new("IntValue", plr)
Fork.Name = "Forky"
local data = getPlayerData(plr)
if data == nil then
data = 0
lifts.Value = data
Fork.Value = 1
end
end
game.Players.PlayerAdded:Connect(givestats)
game.Players.PlayerRemoving:Connect(savePlayerData)
while wait(60) do
for _, plr in pairs (game.Players:GetChildren()) do
savePlayerData(plr)
end
end
Heres the other script:
local LiftingEvent = Instance.new("RemoteEvent", game.ReplicatedStorage)
LiftingEvent.Name = "Lift"
local LastLifted = {}
function PlayerLifted(plr)
if LastLifted[plr.UserId] == nil then
LastLifted[plr.UserId] = os.time()
end
if os.time() - LastLifted[plr.UserId] >= 1 then
plr.leaderstats.Lifts.Value = plr.leaderstats.Lifts.Value + plr.Forky.Value * 3
LastLifted[plr.UserId] = os.time()
end
end
LiftingEvent.OnServerEvent:Connect(PlayerLifted)

1 comment
Ah okay, thanks.