@SomeFedoraGuy is correct and you should follow his advice.
but why can’t you just in the serverscript in the playeradded event, create a variable for the textbutton and then check from there if the textbutton is activated then add the item to the player backpack
You are technically correct, but it’s bad in practice for 2 main reasons:
- You are relying on the server to wait for the children of StarterGui to clone to the player’s PlayerGui.
If you didn’t know, if a player’s Character spawns, every children under the player’s PlayerGui (player.PlayerGui) will destroy itself, and every children under StarterGui will (re)clone inside the player’s PlayerGui unless it is a ScreenGui with the ResetOnSpawn set to false. In this case, it will only clone the first time and wont destroy itself and it’s children.
Because of this, you are forced to wait for these children on CharacterAdded and have to do workarounds.
local function bindScreenGui(player, screenGui)
local imageButton = screenGui:WaitForChild("ImageButton")
imageButton.MouseButton1Click:Connect(function()
print(player, "clicked the ImageButton")
end)
end
game:GetService("Players").PlayerAdded:Connect(function(player)
-- Wait for the player's character to spawn
player.CharacterAdded:Connect(function(character)
-- Make sure the PlayerGui has loaded and parented
local playerGui = player:WaitForChild("PlayerGui")
-- Check if the ScreenGui exists when the player's character spawns
local screenGui = playerGui:FindFirstChild("ScreenGui")
if screenGui == nil then
-- If not, wait for the ScreenGui to spawn
local screenGuiConnection
screenGuiConnection = playerGui.ChildAdded:Connect(function(child)
if child.Name == "ScreenGui" then
screenGuiConnection:Disconnect()
-- Bind the screen gui
bindScreenGui(player, screenGui)
end
end)
else
-- If so, bind the screen gui
bindScreenGui(player, screenGui)
end
end)
end)
Instead of doing this hacky method, let the LocalScript communicate with the server instead using the normal Remotes.
- Dynamically changing Gui is trickier and, in some cases, near impossible to code if you are doing it purely server-side.
You should be modifying the player’s Guis locally. Not only is it easier because you technically don’t have to rely on the server (unless updating information), you also reduce load on the server that should really only be handling updating any important game and data information.
An example of this is an in-game shop menu with different vendors. Say whenever you are near an NPC and hit the key “Q”, you want to update the shop frame to match what the vendor sells. Under the hood, you will need to delete every existing item button and create new item buttons for every item the vendor sells, then make the shop frame visible.
Already, you cannot get the player’s input using UserInputService/ContextActionService on the server alone, you will need to check that in the client and pass it onto the server using a Remote.
Assuming you do the shop frame updating on the server: Bad practice, as I’ve mentioned that the server should really only be handling important stuff to save resources. Imagine handling 100 players with 20 different distinct Gui, you can see how painful that is for you to code and for the server to handle.
Assuming you do the shop frame on the client: Better, but remember that changing things on the client alone does not replicate on the server. In this case, the server wont see any updates to your Guis anyways, making it impossible for the server to connect any events to dynamically changing Guis.
Hope this helps!