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

Why does this PlayerAdded script only work 1/4 of the time?

Asked by 5 years ago

Hello! Does anyone know why the below script only works some of the time? Unless it's the first player to join the server, in which case it seems to always work, otherwise it only works like 1/4 of the time it seems.

local mps = game:GetService("MarketplaceService")
local gamepass = #######
game.Players.PlayerAdded:Connect(function(player)
        if mps:UserOwnsGamePassAsync(player.UserId, gamepass) then
            player.PlayerGui.RegularGui.Open.Visible=false --hides regular gui
            script.VIPGui:Clone().Parent = player.PlayerGui --clones script child vipgui into players gui
        end
end)

1 answer

Log in to vote
1
Answered by 5 years ago

well, it's possible that expressions such as player.PlayerGui are nil, so i'd recommend using WaitForChild() like so

local mps = game:GetService("MarketplaceService")
local gamepass = #######

game.Players.PlayerAdded:Connect(function(player)
        if mps:UserOwnsGamePassAsync(player.UserId, gamepass) then
            player:WaitForChild("PlayerGu"):WaitForChild("RegularGui").Open.Visible=false
            script.VIPGui:Clone().Parent = player.PlayerGui
        end
end)

you could probably make the code a bit cleaner than just putting a bunch of WaitForChild()s next to each other

0
This was it thank you TheAwesomestKingEver 9 — 5y
0
no problem User#23252 26 — 5y
Ad

Answer this question