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 4 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.

-- variables below
local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
local BADGE_ID = 2124499744
local player = script.Parent.Parent
local plr = Players:GetPlayerFromCharacter(BADGE_ID)

while true do
    wait(0.001)
    if plr and BadgeService:UserHasBadgeAsync(plr.UserId, BADGE_ID)then
    print("Welcome!")
    -- basically prints into the output welcome if they have the badge
    else
    player:Kick("You don't have the flashlight!")
    -- if they don't have the badge, then it kicks you from the game.
    end
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.

2 answers

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

I assume the problem is coming from here

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?

local 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 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

I spotted this:


local Players = game:GetService("Players") local BadgeService = game:GetService("BadgeService") local BADGE_ID = 2124499744 local player = script.Parent.Parent local plr = Players:GetPlayerFromCharacter(BADGE_ID) while true do wait(0.001) if plr and BadgeService:UserHasBadgeAsync(plr.UserId, BADGE_ID)then -- you forgot the space print("Welcome!") else player:Kick("You don't have the flashlight!") end 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

Answer this question