Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to verify UserHasBadge before TeleportService?

If the user has the badge, they'll be teleported to the next place. If they don't, they will be denied access. I'm lost on what I forgot to add into the script. Any help on this?

BadgeId = 106566897 --Testing Badge


function onTouched(hit)
	character = hit.Parent
	if character then
		if game:GetService("BadgeService"):UserHasBadge(p.userId, BadgeId) then
        print("The user has this badge")
		print("Teleporting user to next level")
		game:GetService("TeleportService"):Teleport(158451458, player)
    else
        print("The user does not have this badge")
		print("Teleport disabled")
    	end
	end
end

2 answers

✓ Accepted answer

Well, you want to define the player, so what I do is find the player's name in PlayerService. But you also don't want a random object hitting the brick messing your script up.

BadgeId = 106566897 --Testing Badge


function onTouched(hit)
    character = hit.Parent
    if character then
if game.Players:FindFirstChild(character.Name) then --Need to see if it is a player or random object.
local player=game.Players:FindFirstChild(character.Name)
        if game:GetService("BadgeService"):UserHasBadge(player.userId, BadgeId) then
        print("The user has this badge")
        print("Teleporting user to next level")
        game:GetService("TeleportService"):Teleport(158451458, player)
    else
        print("The user does not have this badge")
        print("Teleport disabled")
        end
    end
end
end

The reason your's is not working is because it is not referencing a character in the script. Hit.Parent may be a model or a gun. To do this you must reference a character. As so:

local char = Hit.Parent:FindFirstChild("Humanoid")
local plyr = game.Players:GetPlayerFromCharacter(Hit.Parent)
if char ~= nil and plyr ~= nil then -- ~= nil means not equal to nil.
--Rest of script
end

This discussion is preserved as it was. New answers, comments, and votes are closed.