So this VIP door was working fine until the other day when it started giving me an error.
2. What is the issue? Include screenshots / videos if possible!
I always get stuck on this stuff but so far I have tried calling
local player = game.Players.LocalPlayer
However that didn’t work.
The Error/Error Line (Serverside Script):
local character = player.Character
Workspace.VIPdoor.Script:22: attempt to index nil with ‘Character’
Full Code:
local GamepassId = 12424604
local door = script.Parent
function open()
door.CanCollide = false
end
function close()
door.CanCollide = true
end
function get_player(part)
for _, player in ipairs(game.Players:GetPlayers()) do
if part:IsDescendantOf(player.Character)then
return player
end
end
end
door.Touched:Connect(function(part)
local player = get_player(part)
local character = player.Character
if not player then return end
local allow = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, GamepassId)
if allow then
open()
delay(2, close)
end
end)
Well, you do have a guard clause, but it’s not placed in a useful spot. You assume player is not nil, and try to access the player’s character. So you should move that guard up one line.
You also being verbose in how you search for the player.
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if not player then return end
-- player is not nil
door.Touched:Connect(function(part)
local player = game.Players:GetPlayerFromCharacter(part.Parent)
if not player then return end
local character = player.Character
local allow = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, GamepassId)
if allow then
open()
delay(2, close)
end
end)