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

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

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

✓ Accepted answer

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

2 comments

0

This was it thank you

0

no problem

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