Random error in code

Hey, I am currently making a global leaderboard and I randomly came across this error at the beginning. Not sure what is going on but I keep getting this error

Workspace.Script:25: Expected ‘end’ (to close ‘do’ at line 13), got ]

not sure why the function isn’t ending since I’ve doubled checked the number of ends I have it matches. Can anyone help and see what is causing this error? Don’t mind the amount of comments I have I am learning how to code and I like to document each step. It only triples the amount of time to write code :slight_smile:

-- creates shortcuts and finds the parts
local a1 = script.Parent
local sample = script:WaitForChild("Sample")
local b1 = a1:WaitForChild("ScrollingFrame")
local c1 = b1.WaitForChild("UI")

-- assumes it loads the local data and pulls from leaderboard
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetOrderedDataStore("Leaderboard")

-- refreshes to start the update and creates a loop
wait(5)
while true do
	for i,plr in pairs(game.Players:GetChildren()) do -- this starts the for loop. Its probably repeatidly accessing data from the players
	if plr.UserId > 0 then -- makes sure the user is a real player
		local w = plr.Leaderboard.Cured.Value -- this pulls the value of cured out of the player
		if w then
			pcall(function() -- a function that protects the code and doesnt stop it if there is an error with roblox's servers
				datastore:UpdateAsync(plr.UserID,function(oldVal) --updates old value with a function by first locating player and their ID
				return tonumber(w) -- this is a string that takes the argument and returns it as a number. Will return as nil if the argument isnt a number
				end) -- ends pcall function
			end) -- ends if w then function
		end -- this ends the "if w then" function
	end -- ends if plr.userID function
end -- ends first loop not sure why there is an error

You’re missing 1 end for the in pairs() loop.

You don’t need the bracket in the 4th line from the end
image

It added another end which worked but when I added the rest of the code back with the code I posted here this error came up.

[Workspace.Brick.SurfaceGui.Script:35: Expected ‘)’ (to close ‘(’ at column 15), got ‘,’]

its giving me an error for a comma not sure why this is happening.

I removed it but it gives another error. Im pretty sure that bracket closes out the pcall(function)

1 Like

You shouldn’t have added any new ends because there’s just enough ends to close everything, the only problem was with that bracket. And the pcall already ends one line before.

But doesn’t the bracket before that one end the for loop?

I didn’t see one thing because of your bad indentation and misleading comments in the code, there was indeed an end missing and you need that bracket, sorry.

1 Like

You haven’t showed us line 35. Please give so we can help you.

This is full code up to 35

-- creates shortcuts and finds the parts
local a1 = script.Parent
local sample = script:WaitForChild("Sample")
local b1 = a1:WaitForChild("ScrollingFrame")
local c1 = b1.WaitForChild("UI")

-- assumes it loads the local data and pulls from leaderboard
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetOrderedDataStore("Leaderboard")

-- refreshes to start the update and creates a loop
wait(5)
while true do
	for i,plr in pairs(game.Players:GetChildren()) do -- this starts the for loop. Its probably repeatidly accessing data from the players
			if plr.UserId > 0 then -- makes sure the user is a real player
		local ps = game:GetService("PointsService")--PointsService
		local w = plr.Leaderboard.Cured.Value -- this pulls the value of cured out of the player
		if w then
			pcall(function() -- a function that protects the code and doesnt stop it if there is an error with roblox's servers
				dataStore:UpdateAsync(plr.UserId,function(oldVal) --updates old value with a function by first locating player and their ID
				return tonumber(w) -- this is a string that takes the argument and returns it as a number. Will return as nil if the argument isnt a number
				end) -- ends pcall function
			end) -- ends if w then function
		end -- this ends the "if w then" function
	end -- ends if plr.userID function
end  -- this ends the first loop


--start to fetch data

local smallestFirst = false -- puts biggest first
local numberToShow = 1000 -- range of 0-1000 numbers will show
local minValue = 0
local maxValue = 10e30
local pages = (smallestFirst, numberToShow, minValue, maxValue) --possibly the order it fetches data
while true do
	for i,plr in pairs(game.Players:GetChildren()) do
		if plr.UserId > 0 then
			local w = plr.Leaderboard.Cured.Value
			if w then
				pcall(function()
					datastore:UpdateAsync(plr.UserID,function(oldVal)
					    return tonumber(w)
                                        end)
				end)
			end
		end
	end
end

The second part should look like this. You forgot to close your for i,plr loop.
And don’t forget to add a wait() in your while loop. Otherwise your script will crash.

1 Like

You are making a table in that case. You should use “{}” instead of “()”.

Thanks this actually helped me realize my mistake

1 Like