Badge is not awarding

so i have this script where, if you click on a part, it will show an ending screen and then award you the badge.
but, it doesnt work. the badge doesnt award.

it gives me the error “unable to cast int64”

local TweenService = game:GetService("TweenService")
local badgeservice = game:GetService("BadgeService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage.Events

local plate = game.Workspace:WaitForChild("Plate")
local cd = plate.ClickDetector
local folder = plate.Placement.Items

local sound = game.Workspace.SFX["splat.wav"]

local function checkForItem(player)

	local backpack = player.Backpack

	for _,v in pairs(backpack:GetChildren()) do
		if v:IsA("Tool") then
			print("Player has a tool! Detecting if it's purchased or not!")
			if v.Purchased.Value == false then
				print("You need to purchase the item first!")
			else
				v.Parent = plate.Placement.Items
				v.Handle.Position = Vector3.new(plate.Placement.TP.Position)
				print("Player has purchased item! Giving ending..")
			end
		end
	end
end

function GiveEnding(player)
	for _,v in pairs(folder:GetChildren()) do
		if v:IsA("Tool") then
			wait(0.5)
			Events.GiveEnding:FireServer(player, 4049146362969843, "nyum")
		end
	end
end

function EndingScreen(player)
	for _,v in pairs(folder:GetChildren()) do
		if v:IsA("Tool") then
		print("There is something inside of the folder!")

		wait(0.5)
		script.Parent.Visible = true
		local newpos = UDim2.new(-0.001, 0, -0, 0)
		local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)

		local tween = TweenService:Create(script.Parent, tweenInfo, {Position = newpos})

		tween:Play()
		wait(2)

		if v.name.Value == "Burger" then

			script.Parent.EndingName.Text = "Burger"
			script.Parent.Desc.Text = "nyum"
			
			script.Parent["Ending:"].Visible = true
			sound:Play()
			wait(2)
			script.Parent.EndingName.Visible = true
			sound:Play()
			wait(3)
			sound:Play()
			script.Parent.Desc.Visible = true
			end
		end
	end
end

cd.MouseClick:Connect(function(plr)
	checkForItem(plr)
	GiveEnding(plr)
	EndingScreen(plr)
end)

here is the script (sorry, its long) and here is the script that awards the badge

local badgeservice = game:GetService("BadgeService")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage.Events

Events:FindFirstChild("GiveEnding").OnServerEvent:Connect(function(plr, badgeId, message)
	badgeservice:AwardBadge(plr.UserId, badgeId)
	wait(5)
	plr:Kick(message)
end)

the one that awards the badge is a serverscript in serverscript service.
the one that does the ending screen is a local script inside of a frame in startergui.

ive tried changing it to a server script, but it still doesnt award the badge, yet, it doesnt give the error.

plz help! idk what im doing wrong

Can you provide which line the “unable to cast int64” error is on? Also badges can only be awarded in server side scripts.

1 Like
Events:FindFirstChild("GiveEnding").OnServerEvent:Connect(function(plr, badgeId, message)
	badgeservice:AwardBadge(plr.UserId, badgeId)     --this line

line 7 in the award script

also, it was a local script since i thought when dealing with ui you were always supposed to have it be a local script? lol

That’s true, you should use LocalScripts for UI. The reason why badges can’t be awarded on the client is due to security reasons. Your implementation is still vulnerable to exploits by the way. Exploiters can send their own data through remote events, which in your case means that exploiters can give themselves all the badges.

The problem is that you’re sending the player as an extra argument to the server. The first argument passed to a function connected to .OnServerEvent is always the player that called the event by default. Since you pass the player as an extra argument your badgeId ends up becoming the player parameter you passed into :FireServer(). The error you’re getting means that Luau expects :AwardBade() to take an Int64 as an input, which a player instance can not be casted to.

1 Like

it works now! but i do have a question.

function GiveEnding(player)
	for _,v in pairs(folder:GetChildren()) do
		if v:IsA("Tool") then
			wait(0.5)
			Events.GiveEnding:FireServer(4049146362969843, "nyum")
			print("gave badge") --right here
		end
	end
end

in this function, i added a print after it would give you the badge, why does it print twice when it gives the badge?

and in this function,

function EndingScreen(player)
	for _,v in pairs(folder:GetChildren()) do
		if v:IsA("Tool") then
		print("There is something inside of the folder!") --right here

it prints “there is something inside of the folder!” twice, too.

i dont know if it matters, but i wanted to ask anyways!

Oh that’s because your for loop probably finds multiple tools. Add a break statement after you call :FireServer().

1 Like

correct me if im wrong, but i did it like this and it doesnt work (still prints twice)

function GiveEnding(player)
	for _,v in pairs(folder:GetChildren()) do
		if v:IsA("Tool") then
			wait(0.5)
			Events.GiveEnding:FireServer(4049146362969843, "nyum")
			print("gave ending")
			break --right here?
		end
	end
end

1 Like

Yeah you placed the break statement correctly.

Add a debounce to your script so that you can only get one ending at a time. Maybe your .Clicked event is firing twice. That’s the only cause I can come up with atm.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.