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

How to award badges through a local script?

Is there a way to do this since badges are handled on the server side? Here is my local script if it helps.

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://636749560"
local animtrack = nil
local canplay = true
local playe = game.Players.LocalPlayer
local char = game.Players.LocalPlayer.Character
local badgeID = 637991953
local badgeService = game:GetService("BadgeService")

function processCommand(message,recipient)
	if message == "/e dab" then
		print("part1")
		if canplay then
			print("part2")
			canplay = false
			animtrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)
			print("part3")
			animtrack:Play()
			badgeService:AwardBadge(playe.userId, badgeID)
			print("part4")
			canplay = true
		end
	end
end

playe.Chatted:connect(processCommand)

Thanks!

1 answer

✓ Accepted answer

You can do this just as easy in a ServerScript. If anything, you can have a RemoteEvent/RemoteFunction to connect to that LocalScript and a ServerScript.

So that's what I'm going to do.

Essentially, just replace where you give the badge to the call of the RemoteEvent/RemoteFunction. Which calls the function in the ServerScript where it awards the player.

I tried my best to not improve your code and at the same time get the point across.

Example:

LocalScript-

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://636749560"
local animtrack = nil
local canplay = true
local playe = game.Players.LocalPlayer
local char = playe.Character
local badgeID = 637991953

-- Set up the RemoteEvent (It will be in Lighting)
local Lighting = game:GetService("Lighting")
local Event = Lighting:WaitForChild("RemoteEvent")

function processCommand(message,recipient)
    if message == "/e dab" then
        print("part1")
        if canplay then
            canplay = false
            animtrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)
            animtrack:Play()
		-- Call to the server
		-- We're going to give it some arguments that the AwardBadge function needs 
		-- So it needs the UserID and BadgeID
		Event:FireServer(playe.userId, badgeID) -- Fires the arguments to the server
            canplay = true
        end
    end
end

playe.Chatted:connect(processCommand)

ServerScript:

local badgeService = game:GetService("BadgeService")
-- Set up the RemoteEvent (It will be in Lighting)
local Lighting = game:GetService("Lighting")
local Event = Lighting:WaitForChild("RemoteEvent")

function Award(id, badgeid) -- The function in which awards the player
	badgeService:AwardBadge(id, badgeid) -- Awards the player - BOOM! :D
end

Event.OnServerEvent:connect(Award) -- This listens to when the :FireServer() is called

Sorry if this was spoon feeding, I try my best not too.

3 comments

0

Thanks!

0

It says on line 37 that there is an infinite yield possible? Could you help me with this?

0

Wait NVM figured it out.

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