Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Could someone help me with why this leaderboard script will not work?

I get the error "Expected , got 'end'"

Here is the script:

local ds = game:GetService("DataStoreService"):GetDataStore("--Cash01")

game.Players.PlayerAdded:Connect(function(player)
	local key = "cash-"..player.userId
	local folder = Instance.new("Folder",player)
	folder.Name = "leaderstats"
	local currency = Instance.new("IntValue",folder)
	currency.Name = "Coins"
	currency.Value = 0
	
	local fake1 = Instance.new("StringValue")
	fake1.Name = currency.Name
	fake1.Value = tostring(currency.Value)
	fake1.Parent = folder
	
	while wait() do
		local x = tostring(currency.Value)
		if #x >= 10 then -- Billions
			fake1.Value = x:sub(0,(#x-9)).."."..(x:sub(#x-7,(#x-7))).."B+"
		elseif #x >= 7 then -- Millions
			fake1.Value = x:sub(0,(#x-6)).."."..(x:sub(#x-5,(#x-5))).."M+"
		elseif #x >= 4 then -- Thousands
			fake1.Value = x:sub(0,(#x-3)).."."..(x:sub(#x-2,(#x-2))).."K+"
		else -- If it's not high enough...
			fake1.Value = tostring(currency.Value)
		end
	end
end)	
	local save = ds:GetAsync(key)
	if save then
		currency.Value = save
	end
	currency.Changed:Connect(function()
		ds:SetAsync(key,currency.Value)
	end)
end)

1 comment

0

If my answer worked please accept it. The button should be under my answer. Also could you clarify what you mean by "it won't abbreviate".

1 answer

✓ Accepted answer

This is happening because end) on line 28 closes your event listener. that end) at the bottom is not closing off anything and that is why it gives you an error. You should move lines 29 and 35 inside your event listener.

local ds = game:GetService("DataStoreService"):GetDataStore("--Cash01")

game.Players.PlayerAdded:Connect(function(player)
    local key = "cash-"..player.UserId
    local folder = Instance.new("Folder") -- # parent objects last for better performance
    folder.Name = "leaderstats"
    folder.Parent = player

    local currency = Instance.new("IntValue")
    currency.Name = "Coins"
    currency.Value = ds:GetAsync(key) -- # defaults to 0 if there isn't any data 
    currency.Parent = folder

    local fake1 = Instance.new("StringValue")
    fake1.Name = currency.Name
    fake1.Value = tostring(currency.Value)
    fake1.Parent = folder

    currency.Changed:Connect(function()
        local x = tostring(currency.Value)
        if #x >= 10 then -- Billions
            fake1.Value = x:sub(0,(#x-9)).."."..(x:sub(#x-7,(#x-7))).."B+"
        elseif #x >= 7 then -- Millions
            fake1.Value = x:sub(0,(#x-6)).."."..(x:sub(#x-5,(#x-5))).."M+"
        elseif #x >= 4 then -- Thousands
            fake1.Value = x:sub(0,(#x-3)).."."..(x:sub(#x-2,(#x-2))).."K+"
        else -- If it's not high enough...
            fake1.Value = tostring(currency.Value)
        end
    end)
end)

Notice the script does not save each time the currency value changes. You should not be doing that. I also removed the infinite loop as you only need to check each time the currency changes. Not the default interval, which is ~0.03 seconds.

1 comment

0

Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.