I am making a script that detects if the player has a badge or a gamepass. Once the player joins it should detect if the player has a badge or a gamepass and make the door nvisible (just to see if it works)
I found no answers online so I would be very happy if you could help.
door = script.Parent
badgeholder = door.Parent.BadgeID.Value
gamepassholder = door.Parent.GamepassID.Value
local badgeservice = game:GetService("BadgeService")
local marketplace = game:GetService("MarketplaceService")
game.Players.PlayerAdded:connect(function(player)
if player.Parent:FindFirstChild("Humanoid") then
local p = game.Players:GetPlayerFromCharacter(player.Parent)
if p then
if badgeservice:UserHasBadgeAsync(p.UserId, badgeholder) then
elseif badgeservice:UserHasBadgeAsync(p.UserId, badgeholder) == false then
if marketplace:UserOwnsGamePassAsync(p.UserId, gamepassholder) == false then
door.Transparency = 0
end
end
end
end
end)
oh my theres alot of problems here that im nitpicky about.
First off when doing a Playeradded event, The event returns the player object in game.Players already, so all you need to do is
game.Players.PlayerAdded:Connect(function(plr)
local Char = plr.Character
end)
Next if you want to make a door invisible for a certain player, your gonna need to communicate this to that players client via remote events
Or if you want that player to go through
door, use collision groups
For checking if a user owns a badge, wrap it in a pcall like so
local succ, result = pcall(function()
return BadgeService:UserHasBadge(plr.Userid, BadgeId)
end)
Im on mobile so its a little weird
Hope i helped you, Let me know if you need any more help/advice.
door = script.Parent
badgeholder = door.Parent.BadgeID.Value
gamepassholder = door.Parent.GamepassID.Value
local badgeservice = game:GetService("BadgeService")
local marketplace = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(plr)
local Char = plr.Character
local player = game.Players:GetPlayerFromCharacter(Char)
if player then
if badgeservice:UserHasBadgeAsync(player.UserId, badgeholder) then
elseif badgeservice:UserHasBadgeAsync(player.UserId, badgeholder) == false then
if marketplace:UserOwnsGamePassAsync(player.UserId, gamepassholder) == false then
door.Transparency = 0
end
end
end
end)
It does not make the door part visible without any errors
(and yes I made the value to a random number so I dont have badge or gamepass)
here this is in the PlayerAdded function:
PlayerAdded RETURNS THE PLAYER OBJECT ALREADY FROM GAME.PLAYERS!
This code is pasted in the Playeradded function
local Char = plr.Character -- THIS IS THE CHARACTER
local succ, err = pcall(function() -- THIS CATCHES ERRORS
return BadgeService:UserOwnsBadgeAsync(plr.Userid, BadgeId)
end)
if succ
print("user owns badge")
end
door = script.Parent
badgeholder = door.Parent.BadgeID.Value
gamepassholder = door.Parent.GamepassID.Value
local badgeservice = game:GetService("BadgeService")
local marketplace = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(plr)
local Char = plr.Character
local player = game.Players:GetPlayerFromCharacter(Char)
if player then
if badgeservice:UserHasBadgeAsync(player.UserId, badgeholder) then
elseif badgeservice:UserHasBadgeAsync(player.UserId, badgeholder) == false then
if marketplace:UserOwnsGamePassAsync(player.UserId, gamepassholder) == false then
door.Transparency = 0
end
local Char = plr.Character -- THIS IS THE CHARACTER
local succ, err = pcall(function() -- THIS CATCHES ERRORS
return badgeservice:UserOwnsBadgeAsync(plr.Userid, badgeholder)
end)
if succ then
print("user owns badge")
end
end
end
end)
local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
Players.PlayerAdded:Connect(function(player)
--player returns playerobject in game.Players
local Character = player.Character
local HasBadge, Result = pcall(function()
return BadgeService:HasBadge(player.Userid, badgeid)
end)
if HasBadge then
print("Player has badge, make door visible or transparent!")
end
end)
door = script.Parent
badgeholder = door.Parent.BadgeID.Value
gamepassholder = door.Parent.GamepassID.Value
local badgeservice = game:GetService("BadgeService")
local marketplace = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(plr)
local Char = plr.Character
local player = game.Players:GetPlayerFromCharacter(Char)
if player then
if badgeservice:UserHasBadgeAsync(player.UserId, badgeholder) then
elseif badgeservice:UserHasBadgeAsync(player.UserId, badgeholder) == false then
if marketplace:UserOwnsGamePassAsync(player.UserId, gamepassholder) == false then
door.Transparency = 0
end
end
end
local Character = player.Character
local HasBadge, Result = pcall(function()
return badgeservice:HasBadge(player.Userid, badgeholder)
end)
if HasBadge then
print("Player has badge, make door visible or transparent!")
end
end)
local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
local badgeid = script.Parent.Parent:WaitForChild("BadgeID")
Players.PlayerAdded:Connect(function(player)
--player returns playerobject in game.Players
local Character = player.Character
local HasBadge, Result = pcall(function()
return BadgeService:UserHaaBadgeAsync(player.UserId, badgeid)
end)
if HasBadge then
print("Player has badge, make door visible or transparent!")
end
end)
local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
local badgeid = script.Parent.Parent:WaitForChild("BadgeID")
Players.PlayerAdded:Connect(function(player)
--player returns playerobject in game.Players
local Character = player.Character
local HasBadge, Result = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeid)
end)
if HasBadge then
print("Player has badge, make door visible or transparent!")
end
end)