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

How can you give your players a overhead gui when the click a text button?

Hello. I have been working on a game for quite awhile and I've gotten to a point where I generally don't know what to do next. I am trying to make a script so that when a player press a Text button, they will get an overhead gui with the team name on it. I have looked all over the Dev Forum and YouTube and other websites trying to figure out how I can make this possible and I could not find anything on it. I've tried multiple things One example of something I tried is

script.Parent.MouseButton1Click:Connect(function()
	local clonedgui = game.Workspace.ServerStorage.CatTeam:Clone() -- CatTeam is referencing the name of my overhead gui.
	clonedgui.Parent = game.Workspace:WaitForChild(play.Name).Head
	end
end)
	

I put this in a local script underneath my team button. I think one of my problems is the script needs to refrence the player that clicked it, but I have no idea to approach that.

Thanks so much for taking the time out of your day to read my question.

2 answers

✓ Accepted answer

The script you gave me has one error in terms of scripting but also another error in terms of the game's functionality.

For quite a long time now ROBLOX has strongly recommended and almost enforced players to have FilteringEnabled (FE) in their game, and for a good reason. It prevents exploiters from changing the server through the client. Your script will not work with an FE game.

The first error comes in line 2

local clonedgui = game.Workspace.ServerStorage.CatTeam:Clone()

ServerStorage is a service; it can only be accessed using the DataModel/GetService method. I'm assuming this is what you're trying to access and not a Model or Folder you created in Workspace you named "ServerStorage". If that's the case, carry on...

Otherwise, here's the correct method you should use:

local serverStorage = game:GetService("ServerStorage") -- Get ServerStorage first
local clonedgui = serverStorage:FindFirstChild("CatTeam"):Clone() -- From ServerStorage, find the child with the name "CatTeam" and clone it.

Now let's talk about the real issue: you're trying to do this from the client. This will not work through the client since ServerStorage doesn't replicate its contents to the client and therefore it can't be accessed. You could potentially use ReplicatedStorage which replicates its contents to both client and server. However, the only player who would be able to see it would be the one who ran the script. Therefore, if we want all players to see it, we must first inform the server of this and request a change. To do this we can either use a RemoteFunction or a RemoteEvent

Let me try to guide you through how we would approach this.

First, create a Script in ServerScriptService. Inside that script put in the script below. I've commented it thoroughly so you have an idea of what is going on.

-- Get our needed services
local replicatedStorage = game:GetService("ReplicatedStorage") -- Get the storage which replicates to both client and server
local serverStorage = game:GetService("ServerStorage") -- Get the storage which doesn't replicate to client and where we keep our overhead gui

-- Create a remoteEvent
local remoteEvent = Instance.new("RemoteEvent") -- Create a new remoteEvent using Instance.new()
remoteEvent.Name = "CloneOverheadGui" -- Give it a name so we can access it through the client
remoteEvent.Parent = replicatedStorage -- Parent it to replicatedStorage so we know we can later access it through the client.

local overheadGui = serverStorage:FindFirstChild("CatTeam") -- Let's get our GUI in a variable and clone it afterwards

remoteEvent.OnServerEvent:Connect(function(player) -- OnServerEvent comes with the player as a default parameter (very handy).
    local character = player.Character -- Get the player's character
    if character then -- Check the player's character is here (or that the player isn't dead)
        local head = character:FindFirstChild("Head") -- Get the player's head
        if head then -- if the head is there,
           local clonedgui = overheadGui:Clone() -- Now let's clone it
           clonedgui.Parent = head -- Parent it to the head
        end
    end
end)

-- It's good to have these checks to ensure everything runs smoothly and without errors.

Then, inside the LocalScript, paste this code in. It's fairly simple now that the server is handling the rest.

local replicatedStorage = game:GetService("ReplicatedStorage") -- get replicatedStorage
local overheadRE = replicatedStorage:WaitForChild("CloneOverheadGui") -- find our RemoteEvent using WaitForChild method as "ROBLOX does not guarantee the time or order in which objects are replicated from the server to the client".

script.Parent.MouseButton1Click:Connect(function() -- when player clicks the textButton using the left button on their mouse
    overheadRE:FireServer() -- Fire the remoteEvent. More information on how this works here:
    -- https://developer.roblox.com/en-us/api-reference/function/RemoteEvent/FireServer
end)

If you have any questions let me know. Hope this helps you.

Happy developing!

I think your problem is line 2, You are trying to find ServerStorage through the workspace. Along with you not defying the player, you could do this

local plr = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    local clonedgui = game.ServerStorage.CatTeam:Clone() -- CatTeam is referencing the name of my overhead gui.
    clonedgui.Parent = game.Workspace:WaitForChild(plr.Name).Head
    end
end)

I'm sorry if this is incorrect, I just wanted to see if this would work for you.

Best regards scripting!

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