Attempt to index nil with :Destroy

Hey everyone,
I have a frame with a textlabel inside of it, in ReplicatedStorage. When the player orders something, the frame goes into the UI. For some reason, whenever I press the textbutton after it got parented to the PlayerGui, I keep getting the title as an error. Here are all the scripts:
Also, I tried printing the script.Parent.Parent, it just prints “nil”

Script inside of TextButton in ReplicatedStorage:

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.FinishOrder:FireServer(script.Parent.Parent)
end)

Script inside ServerScriptService:

finishOrder.OnServerEvent:Connect(function(player, uiElement)

currentOrderAmount.Value -= 1

print(uiElement)

finishOrder:FireAllClients(uiElement)

end)

Script inside of StarterGui

finishOrder.OnClientEvent:Connect(function(uiElement)

uiElement:Destroy()
end)

It’s a better way of destroying, also is more sustainable.

1 Like

Still have the issue, does anyone know the solution? I found out it’s because the frame is not visible to the server, how could I fix it?

instead of copying the ui from replicated storage into the player’s ui, i would instead just have the frame inside the player’s ui but set visible to false and then set it to true whenever the button is clicked

StarterGui script:

finishOrder.OnClientEvent:Connect(function(player, gui)
    local Debris = game:GetService("Debris")
    Debris:AddItem(gui, 0.01) -- How long you want it to wait to get removed.
end)

That should work.