Server script not changing frame's visibility

I know that server scripts aren’t supposed to be inside player gui, but some things, like shop and etc were much more optimized and easy to make with server scripts, rather than with 100s of remote events.

Now I have this server script inside a “Back” button

print("Working")
local inv = script:FindFirstAncestor("Inventory")
local main = inv:WaitForChild("MainFrame")
local arm = inv:WaitForChild("ArmorFrame")
local mask = inv:WaitForChild("MaskFrame")
local tit = inv:WaitForChild("TitleFrame")
local inf = inv:WaitForChild("InfoFrame")


script.Parent.MouseButton1Click:Connect(function()
	print("Clicked")
	print(inv.Parent.Parent.Name)
	arm.Visible = false
	mask.Visible = false
	tit.Visible = false
	inf.Visible = false
	main.Visible = true
end)

it prints all print statements, prints the player’s name (I did that to make sure that the script is running inside PlayerGui and not StarterGui. But the frame’s visibility don’t change. What is the issue?

The same script as a local script works, but there are other issues when I put it in local because of replication

This will only work on the server, you will have to use a local script for this. Also, have one script that handles the shop and just have remote events it’s much more optimized.