Hello there.
I’m a skilled builder who is quite new to scripting.
I was testing out UserHasBadgeAsync, and I used the sample code from here.
So it told me to ‘Connect “PlayerAdded” events to the “onPlayerAdded()” function’, so as I said, I was quite new to scripting and I did Players.PlayerAdded:Connect(onPlayerAdded(game.Players.LocalPlayer)). What it did was, it did indeed work well as expected, however the output says Attempt to connect failed: Passed value is not a function. Now why is this happening, and what should I do?
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeID_Blue = 2124659104
local badgeID_Green = 2124659106
local badgeID_Cyan = 2124659110
local badgeID_Orange = 2124663145
local badgeID_Yellow = 2124659114
local badgeID_White = 2124663146
local badgeID_Purple = 2124659115
local Blue = script.Parent:WaitForChild("Blue")
local Green = script.Parent:WaitForChild("Green")
local Cyan = script.Parent:WaitForChild("Cyan")
local Orange = script.Parent:WaitForChild("Orange")
local Yellow = script.Parent:WaitForChild("Yellow")
local White = script.Parent:WaitForChild("White")
local Purple = script.Parent:WaitForChild("Purple")
local function onPlayerAdded(player)
-- Check if the player has the badge
local success, hasBadgeBlue = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_Blue)
end)
local success, hasBadgeGreen = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_Green)
end)
local success, hasBadgeCyan = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_Cyan)
end)
local success, hasBadgeOrange = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_Orange)
end)
local success, hasBadgeYellow = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_Yellow)
end)
local success, hasBadgeWhite = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_White)
end)
local success, hasBadgePurple = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_Purple)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has crystal!")
return
end
if hasBadgeBlue then
Blue.TextLabel.Text = "OWNED"
else
Blue.TextLabel.Text = "LOCKED"
end
if hasBadgeGreen then
Green.TextLabel.Text = "OWNED"
else
Green.TextLabel.Text = "LOCKED"
end
if hasBadgeCyan then
Cyan.TextLabel.Text = "OWNED"
else
Cyan.TextLabel.Text = "LOCKED"
end
if hasBadgeOrange then
Orange.TextLabel.Text = "OWNED"
else
Orange.TextLabel.Text = "LOCKED"
end
if hasBadgeYellow then
Yellow.TextLabel.Text = "OWNED"
else
Yellow.TextLabel.Text = "LOCKED"
end
if hasBadgeWhite then
White.TextLabel.Text = "OWNED"
else
White.TextLabel.Text = "LOCKED"
end
if hasBadgePurple then
Purple.TextLabel.Text = "OWNED"
else
Purple.TextLabel.Text = "LOCKED"
end
end
-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded(game.Players.LocalPlayer))
The issue is you are calling onPlayerAdded(game.Players.LocalPlayer) which returns nothing. Since this doesn’t return, you’re essentially doing Players.PlayerAdded:Connect(nil).
Also, since this is local, and only for the current player, you don’t need to mess with PlayerAdded at all. Just running the code in the LocalScript will suffice.
The script could also be improved in unrelated ways, such as storing the color names with IDs in a dictionary and iterating over it to reduce code repetition.
wait();local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeID_Blue = 2124659104
local badgeID_Green = 2124659106
local badgeID_Cyan = 2124659110
local badgeID_Orange = 2124663145
local badgeID_Yellow = 2124659114
local badgeID_White = 2124663146
local badgeID_Purple = 2124659115
local Blue = script.Parent:WaitForChild("Blue")
local Green = script.Parent:WaitForChild("Green")
local Cyan = script.Parent:WaitForChild("Cyan")
local Orange = script.Parent:WaitForChild("Orange")
local Yellow = script.Parent:WaitForChild("Yellow")
local White = script.Parent:WaitForChild("White")
local Purple = script.Parent:WaitForChild("Purple")
local player = game.Players.LocalPlayer
local success, hasBadgeBlue = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_Blue)
end)
local success, hasBadgeGreen = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_Green)
end)
local success, hasBadgeCyan = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_Cyan)
end)
local success, hasBadgeOrange = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_Orange)
end)
local success, hasBadgeYellow = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_Yellow)
end)
local success, hasBadgeWhite = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_White)
end)
local success, hasBadgePurple = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID_Purple)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has crystal!")
return
end
if hasBadgeBlue then
Blue.TextLabel.Text = "OWNED"
else
Blue.TextLabel.Text = "LOCKED"
end
if hasBadgeGreen then
Green.TextLabel.Text = "OWNED"
else
Green.TextLabel.Text = "LOCKED"
end
if hasBadgeCyan then
Cyan.TextLabel.Text = "OWNED"
else
Cyan.TextLabel.Text = "LOCKED"
end
if hasBadgeOrange then
Orange.TextLabel.Text = "OWNED"
else
Orange.TextLabel.Text = "LOCKED"
end
if hasBadgeYellow then
Yellow.TextLabel.Text = "OWNED"
else
Yellow.TextLabel.Text = "LOCKED"
end
if hasBadgeWhite then
White.TextLabel.Text = "OWNED"
else
White.TextLabel.Text = "LOCKED"
end
if hasBadgePurple then
Purple.TextLabel.Text = "OWNED"
else
Purple.TextLabel.Text = "LOCKED"
end