Hii, so I wanted to try and make a script, where if the player has a certain badge they can join the game. If they don't have it then it kicks them with the message of saying you don't have this badge or whatever.
01 | -- variables below |
02 | local Players = game:GetService( "Players" ) |
03 | local BadgeService = game:GetService( "BadgeService" ) |
04 | local BADGE_ID = 2124499744 |
05 | local player = script.Parent.Parent |
06 | local plr = Players:GetPlayerFromCharacter(BADGE_ID) |
07 |
08 | while true do |
09 | wait( 0.001 ) |
10 | if plr and BadgeService:UserHasBadgeAsync(plr.UserId, BADGE_ID) then |
11 | print ( "Welcome!" ) |
12 | -- basically prints into the output welcome if they have the badge |
13 | else |
14 | player:Kick( "You don't have the flashlight!" ) |
15 | -- if they don't have the badge, then it kicks you from the game. |
16 | end |
17 | end |
When I test the game it says in output: -- Unable to cast value to object
I'm using someone else's badge before I use my own. And the script is placed into StarterPack, idk if it has to do something with that. But, it's a regular script.
I read this website already, btw which is here.
I don't know what is wrong with the script, or if it is just roblox. I hope you can use other player's badges.
I assume the problem is coming from here
1 | local plr = Players:GetPlayerFromCharacter(BADGE_ID) |
BADGE_ID
is a number, this method expects a character object, to get the player from, so you need the character first
Also, don't you already have the player?
1 | local player = script.Parent.Parent |
Why are you creating another one
I spotted this:
01 | local Players = game:GetService( "Players" ) |
02 | local BadgeService = game:GetService( "BadgeService" ) |
03 | local BADGE_ID = 2124499744 |
04 | local player = script.Parent.Parent |
05 | local plr = Players:GetPlayerFromCharacter(BADGE_ID) |
06 |
07 | while true do |
08 | wait( 0.001 ) |
09 | if plr and BadgeService:UserHasBadgeAsync(plr.UserId, BADGE_ID) then -- you forgot the space |
10 | print ( "Welcome!" ) |
11 |
12 | else |
13 | player:Kick( "You don't have the flashlight!" ) |
14 |
15 | end |
16 | end |
It might just be a typo and not the problem but if it is please accept this as your answer.
All the best fruits,
PrismaticFruits - obviously a very talented scripter