Click a GUI button, and have the gui image above a player's head change?

I’m trying to update the image in the GUI above the character’s head by clicking a button in the player’s gui. I can’t get this to work, and it’s returning no errors unfortunately! Any solutions? I’m quite new to scripting, so I probably donked something up LOL.


The button on the right has an “ImageURL” attribute that holds the id of the image to be transplanted into the “CommunicationImage”

-- UpdateCommunicationImage Script
-- This script updates the player's Head.MemberGui.CommunicationImage.Image to the image of the button clicked.

local Players = game:GetService("Players")

-- Function to update the communication image
local function updateCommunicationImage(player, imageUrl)
	-- Wait for the player's character to load
	local character = player.Character or player.CharacterAdded:Wait()
	-- Ensure the character has a head
	local head = character:FindFirstChild("Head")
	if not head then return end
	-- Find the MemberGui in the head
	local memberGui = head:FindFirstChild("MemberGui")
	if not memberGui then return end
	-- Find the CommunicationImage inside the Frame
	local communicationImage = memberGui:FindFirstChild("CommunicationImage")
	if not communicationImage then return end
	-- Update the image
	communicationImage.Image = imageUrl
end

-- Connect to the PlayerAdded event to handle players as they join
Players.PlayerAdded:Connect(function(player)
	-- Connect to the player's character appearance
	player.CharacterAdded:Connect(function(character)

		local button = game.StarterGui.CustomizeGui.Menu.ExamplePage.ScrollingFrame:FindFirstChild("Example", true)
        if button then
            button.MouseButton1Click:Connect(function()
                -- Assuming the button has an attribute "ImageUrl" storing the URL of the image to set
                local imageUrl = button:GetAttribute("ImageUrl")
                updateCommunicationImage(player, imageUrl)
            end)
        end
	end)
end)

-- Iterate through all existing players and attach the logic
for _, player in Players:GetPlayers() do
	player.CharacterAdded:Connect(function(character)
	end)
end
4 Likes

Create a LocalScript inside the button in the player’s GUI. In the LocalScript, use the Activated event to detect when the button is clicked. Inside the Activated event function, access the player’s character using the game.Players.LocalPlayer.Character property. Find the GUI above the character’s head. You can do this by searching for a specific part or using a custom tag. Once you have the GUI, update its image by changing the Imageproperty to the desired image asset ID.

Try using this code:

local button = script.Parent

local function onButtonActivated()
    local player = game.Players.LocalPlayer
    local character = player.Character

    -- Find the GUI above the character's head
    local guiAboveHead = character:FindFirstChild("GUIAboveHead")
    if guiAboveHead then
        -- Update the image in the GUI
        guiAboveHead.Image = "
   end
end
button.Activated:Connect(onButtonActivated)

Make sure to replace “GUIAboveHead” with the actual name or tag of the GUI above the character’s head. Also, place asset ID of the image you want to display where it says “guiAboveHead.Image = “

4 Likes

With a teensy bit of tweaking, I got this to work! Fantastic!! Thank you so much!

1 Like

Use MouseButton1Click instead of Activated.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.