How would I fix this badge script?

So i want to award a badge, once you press the proximity prompt

local Hinge = script.Parent.Hinge
local opened = false
local badgeService = game:GetService("BadgeService")
local badgeID = 1234567890

function Open()
	if opened == false then
		opened = true
		for i = 1, 10 do
			script.Parent:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.rad(9), 0))
			game.Players.PlayerAdded:Connect(function(plr)
				if not badgeService:UserHasBadgeAsync(plr.UserId, badgeID) then
					badgeService:AwardBadge(plr.UserId, badgeID)	
				end
			end)
			wait()
		end
	else
		opened = false
		for i = 1, 10 do
			script.Parent:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.rad(-9), 0))
			wait()
		end
	end
end

script.Parent.Door.ProximityPrompt.Triggered:Connect(Open)

what do i need to change?

why are u using game.Players.PlayerAdded?! Did u just simply copy it from another script?

It looks like you’re connecting to the PlayerAdded event 10 times every time the door is opened. Might be better to move that out of the Open function and just add an if (opened == true) then and awarding the badge to new players that way.

It appears to me like you’re trying to give the badge to everyone in the server, so after your for i = 1, 10 do loop you could loop over all players in the game and award them the badge like so:

for _, plr in pairs(game.Players:GetPlayers()) do
	if not badgeService:UserHasBadgeAsync(plr.UserId, badgeID) then
		badgeService:AwardBadge(plr.UserId, badgeID)	
	end
end

Overall the script could look like this:

local Hinge = script.Parent.Hinge
local opened = false
local badgeService = game:GetService("BadgeService")
local badgeID = 1234567890

game.Players.PlayerAdded:Connect(function(plr)
	if not badgeService:UserHasBadgeAsync(plr.UserId, badgeID) and opened then
		badgeService:AwardBadge(plr.UserId, badgeID)	
	end
end)

function Open()
	if opened == false then
		opened = true
		for i = 1, 10 do
			script.Parent:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.rad(9), 0))
			wait()
		end
		
		for _, plr in pairs(game.Players:GetPlayers()) do
			if not badgeService:UserHasBadgeAsync(plr.UserId, badgeID) then
				badgeService:AwardBadge(plr.UserId, badgeID)	
			end
		end
	else
		opened = false
		for i = 1, 10 do
			script.Parent:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.rad(-9), 0))
			wait()
		end
	end
end

script.Parent.Door.ProximityPrompt.Triggered:Connect(Open)
1 Like

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