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

Script deleting a GUI but not cloning a new one into the PlayerGui?

I'm creating a custom player list for my game, which runs off of RemoteEvents. When I fire the event it should create a Frame for every player, which it does successfully, and updates the GUI in the storage.

local lastPosition = -30

game.ServerScriptService.RefreshPlayerList.OnServerEvent:connect(function()
	removeGUIs()
	lastPosition = -30
	local gui = game.ServerScriptService.CustomPlayerList
	for i,v in pairs(game.Players:GetPlayers()) do
		local example = gui.ScrollingFrame.Example:Clone()
		example.Parent = gui.ScrollingFrame
		example.Position = UDim2.new(0,0,0,lastPosition + 30)
		lastPosition = lastPosition + 30
		example.Player.Text = v.Name
		example.Rank.Text = v:GetRoleInGroup(2654781)
		example.Visible = true
		example.Name = v.Name
		example.Parent.CanvasSize = UDim2.new(0,0,0,lastPosition + 30)
	end
end)

But then I try to remove all the player's GUI's, which it does, and then try add the one from the storage, it doesn't clone..

local function removeGUIs()
	for i,v in pairs(game.Players:GetChildren()) do
		v.PlayerGui:FindFirstChild("CustomPlayerList"):Destroy()
	end
	print("removed")
	wait()
	--From here
	local newGui = game.ServerScriptService.CustomPlayerList:Clone()
	for u,x in pairs(game.Players:GetPlayers()) do
		newGui.Parent = x.PlayerGui
		print("Removed from "..x.Name)
	end
	--To here
	print("added") --Yet this prints...
end

1 answer

✓ Accepted answer

You're only creating one GUI.

On line 8 of the second script, you'll notice the newGui isn't in the loop, meaning it only gets created once. The loop on line 9 runs and gives this one GUI to all the players, but there's only one. So try moving line 8 below line 9,

local function removeGUIs()
    for i,v in pairs(game.Players:GetPlayers()) do
        v.PlayerGui:FindFirstChild("CustomPlayerList"):Destroy()
    end
    print("removed")
    wait()
    --From here
    for u,x in pairs(game.Players:GetPlayers()) do
        local newGui = game.ServerScriptService.CustomPlayerList:Clone()
        newGui.Parent = x.PlayerGui
        print("Removed from "..x.Name)
    end
    --To here
    print("added") --Yet this prints...
end
Secondly, I think you already know this but scripts can't access GUIs in the PlayerGui that the script didn't create itself. If you're using a Local Script, Local Scripts Can't access PlayerGui.

Sorry if that didn't work, this was spawned from one problem I saw and a short skim through the scrips.

Good Luck!

1 comment

0

Psst, this is a script running off of RemoteEvents! But yeah, this worked in a way, but I had to put ALL the functions into the main one, but thanks for that.

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