How To Grant A Badge When A Player Gets Enough Of An In game Currency?

After More Than 24 Hours Im Still Confused And This Is What Is Stopping Me From Finishing This.

I Have A Script In Workspace That Makes A Leaderboard Stat Named Candy

Here Is A Snipet For Example Its Related To Trick Or Treating.

local candy = p.leaderstats.Candy.Value
local houses = playersHouses[p]

pcall(function()
	ds:SetAsync(p.UserId, {candy, houses})
end)

end

game.Players.PlayerAdded:Connect(function(p)

local ls = Instance.new("Folder", p)
ls.Name = "leaderstats"

local candy = Instance.new("IntValue", ls)
candy.Name = "Candy"


local candyData = 0
local housesData = {}

Anyways Now There Is A Candy Leader stat And I Want To Grant A Badge At Certain Candy Amounts.
Also it must be where it can go past the number and they still get the badge.

At 750 Candies — 2124857939
At 2500 Candies — 2124857942
At 5000 Candies — 2124857941

What do i do ?

This Is My Current Code But Makes No Sense And I Dont know how to add several ids onto this or do i duplicate the script several times to add more Ids ?. Is This Working

?wdadadawdad

Ive Been Working On This Ages So Please Help :grinning:

what’s the error u’ve been having here

1 Like

Probaly

local First: Value here -- ID of the Badge
local Second: Value here
local Third: ANother value here

candy.Changed:Connect(function(newVal)
if newVal >= 750 then --- Award first Badge
badges:AwardBadge(player.UserId, First)

       else if newVal >= 2500 then -- Award the Second badge
     badges:AwardBadge(player.UserId, Second)

            else if newVal >= 2500 then -- Award the Third badge
                 badges:AwardBadge(player.UserId, Third)

             end
       end  
end

are you trying to do something like this?

1 Like

You dont need the local badgeId, just put the number in the spot

badges:AwardBadge(player.UserId, 2124857939)
--another badge would be
badges:AwardBadge(player.UserId, 5195103921)

if i put this in would it work ?

Yes


 i think a [Module] would be a better idea to Store Badge Ids
'[because imagine 500 badges] 500 local variable? lol nooo'
2 Likes

I made it a server script service script and it didnt work
i lowered it down to 46 candies so i would have the same amount and none was granted.
also i changed Candy And candy and tried both.
DWADWADAWDAD

Whats wrong ?

1 Like

I see the error here , Look:
Incorrect:

local First : 2124855570
Correct
local First = 2124855570 

=
not " : "

1 Like

so would this now grant the badges with my candy leaderboard stat in game ?

should i write candy how it looks on the leaderboard

Candy

so would it work ?

yes, sorry, now will work xD i accidentallyplaced " : "

1 Like

Variables are something kinda personal, you choose your Reference (Variable Name)… i can’t really help with that

Candy = Something
Cand = OtherThing
Can = SomethingElse
whatever, really depends…
In the end variables are just a reference, don’t forget that

for Example

local CandyVariable = Instance.new("IntValue", player)
CandyVariable.Name = "Candy"
CandyVariable.Value = 0

See, CandyVariable is a Variable, but the Intvalue i called by another name so

you must acess the Intvalue.Value to work

Ex:

player.Candy.Value 

it didnt work again
Ignore coins
this is what i mean by Candy
when it hits 46 or whatever number i want it to grant a badge
then 47 another
then 48 another
the code didnt work :frowning: it says there are syntax errors though located in image 2

dawdawdadadad

Image 1 Above

Image 2 Below

the player And Candy + badges text is red underlighted. you cant screenshot it.

This code handles the trick or treating system and the candy leaderboard
how do i make it work for this script ?

local dss = game:GetService(“DataStoreService”)
local ds = dss:GetDataStore(“TrickOrTreat”)

local playersHouses = {}

function saveData(p)

local candy = p.leaderstats.Candy.Value
local houses = playersHouses[p]

pcall(function()
	ds:SetAsync(p.UserId, {candy, houses})
end)

end

game.Players.PlayerAdded:Connect(function(p)

local ls = Instance.new("Folder", p)
ls.Name = "leaderstats"

local candy = Instance.new("IntValue", ls)
candy.Name = "Candy"


local candyData = 0
local housesData = {}


	pcall(function()
	
	local data = ds:GetAsync(p.UserId)
	
	candyData = data[1]
	housesData = data[2]
end)
playersHouses[p] = housesData

candy.Value = candyData

end)

game.Players.PlayerRemoving:Connect(saveData)

game:BindToClose(function()

for i, p in pairs(game.Players:GetPlayers()) do

	spawn(saveData(p))
end

end)

for i, house in pairs(script.Parent:GetChildren()) do

if house:IsA("Model") and house:FindFirstChild("Doorbell") then
	
	local doorOpen = false
	
	local cd = house.Doorbell.ClickDetector
	
	
	cd.MouseClick:Connect(function(p)
		
		if not table.find(playersHouses[p], house.Name) and not doorOpen then
			
			doorOpen = true
			
			
			local character = game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())].Character
			character.Archivable = true
			character = character:Clone()
			
			character:SetPrimaryPartCFrame(house.Door.CFrame - house.Door.CFrame.LookVector * 2)
			
			wait(math.random(1, 3))
			
			character.Parent = workspace
			
			
			local cf = house.Door.CFrame
			house.Door.CFrame = cf * CFrame.Angles(0, math.rad(90), 0) + (cf.LookVector * house.Door.Size.X/2) + (cf.RightVector * house.Door.Size.X/2)
			
			
			p.leaderstats.Candy.Value += 1
			
			
			wait(5)
			
			
			house.Door.CFrame = cf
			character:Destroy()
			table.insert(playersHouses[p], house.Name)
			doorOpen = false
		end
	end)
end

end


Candy.Changed:Connect(function(newVal)
	if newVal >= 750 then --- Award first Badge
		badges:AwardBadge(player.UserId, First)

	else if newVal >= 2500 then -- Award the Second badge
			badges:AwardBadge(player.UserId, Second)

		else if newVal >= 2500 then -- Award the Third badge
				badges:AwardBadge(player.UserId, Third)

			end
		end  
	end
end) ----------------  Close the Function ( This was Missing )
1 Like

local First = 2124855570 – ID of the Badge
local Second = 2124849605
local Third = 2124703048

Candy.Changed:Connect(function(newVal)
if newVal >= 46 then — Award first Badge
badges:AwardBadge(player.UserId, First)

   else if newVal >= 46 then -- Award the Second badge
 **badges**:AwardBadge(**player**.UserId, Second)

        else if newVal >= 46 then -- Award the Third badge
             **badges**:AwardBadge(**player**.UserId, Third)

         end
   end  

end

IF BOLD = HIGHLIGHTED RED OR HAS A ******** Around It

Syntax Error Expected End To Close Function At Line 5 got did you forget to close then at line 12 ???

read the commentary Above ----- ^

Need to close the function with

end)

close the function ? do i put end) ?

dawdadad

Would this work ? and give all badges if i had 46 candies ?

Would this work ? also with the trick or treat handler ?
Would it work with this script ?

local dss = game:GetService(“DataStoreService”)
local ds = dss:GetDataStore(“TrickOrTreat”)

local playersHouses = {}

function saveData(p)

local candy = p.leaderstats.Candy.Value
local houses = playersHouses[p]

pcall(function()
	ds:SetAsync(p.UserId, {candy, houses})
end)

end

game.Players.PlayerAdded:Connect(function(p)

local ls = Instance.new("Folder", p)
ls.Name = "leaderstats"

local candy = Instance.new("IntValue", ls)
candy.Name = "Candy"


local candyData = 0
local housesData = {}


	pcall(function()
	
	local data = ds:GetAsync(p.UserId)
	
	candyData = data[1]
	housesData = data[2]
end)
playersHouses[p] = housesData

candy.Value = candyData

end)

game.Players.PlayerRemoving:Connect(saveData)

game:BindToClose(function()

for i, p in pairs(game.Players:GetPlayers()) do

	spawn(saveData(p))
end

end)

for i, house in pairs(script.Parent:GetChildren()) do

if house:IsA("Model") and house:FindFirstChild("Doorbell") then
	
	local doorOpen = false
	
	local cd = house.Doorbell.ClickDetector
	
	
	cd.MouseClick:Connect(function(p)
		
		if not table.find(playersHouses[p], house.Name) and not doorOpen then
			
			doorOpen = true
			
			
			local character = game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())].Character
			character.Archivable = true
			character = character:Clone()
			
			character:SetPrimaryPartCFrame(house.Door.CFrame - house.Door.CFrame.LookVector * 2)
			
			wait(math.random(1, 3))
			
			character.Parent = workspace
			
			
			local cf = house.Door.CFrame
			house.Door.CFrame = cf * CFrame.Angles(0, math.rad(90), 0) + (cf.LookVector * house.Door.Size.X/2) + (cf.RightVector * house.Door.Size.X/2)
			
			
			p.leaderstats.Candy.Value += 1
			
			
			wait(5)
			
			
			house.Door.CFrame = cf
			character:Destroy()
			table.insert(playersHouses[p], house.Name)
			doorOpen = false
		end
	end)
end

end

For the candy leaderstat ?

dawdawdadadad