I’m currently using this tutorial, and I’m getting the error: Expected ‘)’ to close ‘(’ at line 23, got ‘plr’. What am I doing wrong?
local players = game:GetService(“Players”)
local dataStoreService = game:GetService(“DataStoreService”)
local saveDataStore = dataStoreService:GetDataStore(“SaveDataTest”)
local function savePlrData(plr)
local success,err = pcall(function()
local saveData = {}
for _,stat in pairs(plr.leaderstats:GetChildren()) do
saveData[stat.Name] = stat.Value
end
saveDataStore:SetAsync(plr.UserId,saveData)
end)
if not success then return err end
end
players.PlayerAdded:connect(function(plr) --This is line 23
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = plr
local stage = Instance.new("IntValue")
stage.Name = "Name"
stage.Parent = stats
local data = saveDataStore:GetAsync(plr.UserId)
if data then
print(data.Stage)
for _, stat in pairs(stats:GetChildren()) do
stat.Value = data[stat.Name]
end
else
print(plr.Name .. "has no data")
end
end
plr.CharacterAdded:connect(function(char) --The error is underlining "plr" on this line
local humanoid,hrp = char:WaitForChild("Humanoid"),char:WaitForChild("HumanoidRootPart")
wait()
if humanoid and hrp then
if stage.Value ~= 0 then
local part = workspace.ObbyStages:FindFirstChild(stage.Name)
hrp.CFrame = part.CFrame + Vector3.new(0,1,0)
end
end
end)
players.PlayerRemoving:connect(function(plr)
local err = savePlrData(plr)
if err then print(err) end
end)
game:BindToClose(function()
for _,plr in pairs(players:GetPlayers()) do
local err = savePlrData(plr)
if err then print(err) end
I had looked back at the tutorial and they person who had made it had set it up like that. Once I had removed it, I was getting more errors. I’m very new to Lua, so I’m pretty confused by all of this.
I think I got your code cleaned up, but you’ll still need an autosaving feature.
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local saveDataStore = dataStoreService:GetDataStore("SaveDataTest")
local function savePlrData(plr)
local saveData = {}
for _,stat in pairs(plr.leaderstats:GetChildren()) do
saveData[stat.Name] = stat.Value
end
local success, err = pcall(function()
saveDataStore:SetAsync(plr.UserId,saveData)
end)
if(success)then
print("Saved "..plr.Name)
else
warn(err)
return err
end
end
players.PlayerAdded:Connect(function(plr) --This is line 23
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = plr
local stage = Instance.new("IntValue")
stage.Name = "Name"
stage.Parent = stats
local data = saveDataStore:GetAsync(plr.UserId)
if data then
print(data.Stage)
for _, stat in pairs(stats:GetChildren()) do
stat.Value = data[stat.Name]
end
else
print(plr.Name .. "has no data")
end
end
plr.CharacterAdded:Connect(function(char) --The error is underlining "plr" on this line
local humanoid,hrp = char:WaitForChild("Humanoid"),char:WaitForChild("HumanoidRootPart")
wait()
if humanoid and hrp then
if stage.Value ~= 0 then
local part = workspace.ObbyStages:FindFirstChild(stage.Name)
hrp.CFrame = part.CFrame + Vector3.new(0,1,0)
end
end
end)
end)
players.PlayerRemoving:Connect(function(plr)
local err = savePlrData(plr)
if err then print(err) end
end)
game:BindToClose(function()
for _,plr in pairs(players:GetPlayers()) do
local err = savePlrData(plr)
if err then print(err) end
end
end)