Script working in studio but not working in game

I have a script and it works fine in studio but in the developer logs in-game it says “Acessories is not a valid member of PlayerGui”
image

This is the full script:

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local hatUI = game.ServerStorage.BadgeHats


local BloxxerCapBadge = 3876863929533148




local function onPlayerAdded(player)
	
	
	local success, hasBadge = pcall(BadgeService.UserHasBadgeAsync, BadgeService, player.UserId, BloxxerCapBadge)

	if not success then
		warn("Error while checking if player has badge")
		return
	end

	if hasBadge then
		print("has badge")
		local bloxxerUI = hatUI.BloxxerCap:Clone()
		print("cloned")
		local PlayerGui = player:WaitForChild("PlayerGui")
	    print("giventoplayer1")
		bloxxerUI.Parent = PlayerGui.Accessories.HatFrame.ScrollingFrame
		print("giventoplayer2")
	end
	
end

game.Players.PlayerAdded:Connect(onPlayerAdded)


For some reason, your Accessories gui doesn’t exist in the PlayerGui when it tries to access it in your code. Assuming this code is on the server, it’s likely because their character hasn’t spawned yet when this runs, so the gui hasn’t been replicated from StarterGui yet.

You could probably fix this with a simple :WaitForChild, like so:

		bloxxerUI.Parent = PlayerGui:WaitForChild("Accessories").HatFrame.ScrollingFrame
1 Like

I realised that a few minutes ago, thanks for the help though!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.