Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Badge Only Game not working when tested?? Says "Can't cast value to object"?

Asked by 5 years ago

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
02local Players = game:GetService("Players")
03local BadgeService = game:GetService("BadgeService")
04local BADGE_ID = 2124499744
05local player = script.Parent.Parent
06local plr = Players:GetPlayerFromCharacter(BADGE_ID)
07 
08while 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
17end

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.

2 answers

Log in to vote
1
Answered by
starmaq 1290 Moderation Voter
5 years ago

I assume the problem is coming from here

1local 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?

1local player = script.Parent.Parent

Why are you creating another one

0
I realized that, after my friend pointed it out a hour after I posted this. Sorry! I was tired, and I didn't realize lol. panichub 29 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

I spotted this:

01local Players = game:GetService("Players")
02local BadgeService = game:GetService("BadgeService")
03local BADGE_ID = 2124499744
04local player = script.Parent.Parent
05local plr = Players:GetPlayerFromCharacter(BADGE_ID)
06 
07while 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
16end

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

Answer this question