How to make it so it gives a badge when a player sits in a chair

How to make it so it gives a badge when a player sits in a chair

1 Like

Im currently using this script and it does not work

local BadgeService = game:GetService("BadgeService")
local BadgeId = 2648809456390630

local seat = script.Parent

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local occupant = seat.Occupant
	if occupant then
		local humanoid = occupant.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid then
			if not BadgeService:UserHasBadge(humanoid.Parent.UserId, BadgeId) then
				BadgeService:AwardBadge(humanoid.Parent.UserId, BadgeId)
			end
		end
	end
end)

You’ll need to use :GetPlayerFromCharacter(), as you can only award badges to the Player instance, not the Player’s character.

No such property. You can only get the Player’s UserId from the Player’s instance. Use the mentioned method to get the Player’s instance.

1 Like

Do you think this will work now?

local BadgeService = game:GetService("BadgeService")
local BadgeId = 2648809456390630

local seat = script.Parent

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local occupant = seat.Occupant
	if occupant then
		local humanoid = occupant.Parent:GetPlayerFromCharacter("Humanoid")
		if humanoid then
			if not BadgeService:UserHasBadge(humanoid.Parent.UserId, BadgeId) then
				BadgeService:AwardBadge(humanoid.Parent.UserId, BadgeId)
			end
		end
	end
end)
1 Like

Uhh, I don’t see any changes…

I’ve edited my reply above because I mixed up a method. Try viewing my earlier reply again!

1 Like

I changed this part

local humanoid = occupant.Parent:GetPlayerFromCharacter("Humanoid")
1 Like
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BadgeId = 2648809456390630

local seat = script.Parent

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local occupant = seat.Occupant
	if occupant and occupant.Parent then
        local player = Players:GetPlayerFromCharacter(occupant.Parent)
		if player then
			if not BadgeService:UserHasBadge(player.UserId, BadgeId) then
				BadgeService:AwardBadge(player.UserId, BadgeId)
			end
		end
	end
end)
2 Likes

This method is called from the Player service. Try placing this variable at the top of your Script:

local playerService = game:GetService("Players")

and replacing this line:

with:

local playerSeated = playerService:GetPlayerFromCharacter(occupant.Parent)
if playerSeated then
   BadgeService:AwardBadge(playerSeated.Parent.UserId, BadgeId)
end

If you were to look at the documentation of Seat.Occupant, you’ll see that the Occupant property already references the Player’s humanoid. Therefore, you can just validate if the Humanoid exists by checking if the variable occupant already exists or not.

Would recommend for you to ask which portion of your Script went wrong in the future. You aren’t really supposed to be asking for free Scripts anyways.

1 Like

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