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

Any reasons why this script that changes the transparency isn't working?

I made a script that is supposed to change the transparency of an ImageLabel named "Nut1" in StarterGui to 0 when a part named "NutGiver" is touched by the player. The script does not change the tranparency of this ImageLabel.

I wrote a few print statements to debug the code. It outputs: "NutGiver touched by player: BunjiBloxxer", then it prints: "ImageLabel transparency before change: 0.7", then "ImageLabel transparency set to 0", then "ImageLabel transparency after change: 0"

According to the output, the script should be working correctly but it isn't.

EDIT: I did some testing and it turns out the ImageLabel transparency does change to 0 after I touch the parts, but only after I die in game. Does anyone know why?

local ImageLabel = game.StarterGui.Nuts.Nut1
local NutGiver = script.Parent

NutGiver.Touched:Connect(function(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if player then
		print("NutGiver touched by player: " .. player.Name)
		print("ImageLabel transparency before change: " .. ImageLabel.ImageTransparency)
		ImageLabel.ImageTransparency = 0
		print("ImageLabel transparency set to 0")
		print("ImageLabel transparency after change: " .. ImageLabel.ImageTransparency)
	else
		print("NutGiver touched by non-player object")
	end
end)

3 answers

✓ Accepted answer

A likely cause is that you are doing this in a server script and you are modifying the starter GUI and not the actual GUI the client has.

EDIT: I did some testing and it turns out the ImageLabel transparency does change to 0 after I touch the parts, but only after I die in game. Does anyone know why?

Because you're modifying the objects in the StarterGui. On death + respawn, the GUI will be reloaded for the player.

You could approach this in different ways, but here's a suggestion:

  1. Look over the documentation here.

  2. You likely want to use Server -> Client communication for this. See this.

And a more detailed breakdown if you need it:

  1. Create a "RemoteEvent" object and put it somewhere replicated across client/server. ReplicatedStorage was born for this. I usually create a folder to store my events, so let's create a folder called "Events" and add an event to it. Let's be verbose and call the event "NutGiverTransparencyEvent".

Now, somewhere on the server side, let's keep the .Touched logic there. It looks like in your script, NutGiver is the parent of the script. So:

-- Server Side Code
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TransparencyEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("NutGiverTransparencyEvent")
local NutGiver = script.Parent

NutGiver.Touched:Connect(function(part)
   local Player = Players:GetPlayerFromCharacter(part.Parent)
   if Player then
      print("NutGiver touched by player: " .. Player.Name)
      TransparencyEvent:FireClient(Player)
   end
end)

Now, let's make a new client script. Make a LocalScript object and put it somewhere. The GUI you want to change would be fine. Specifically, let's just make this LocalScript a child of game.StarterGui.Nuts.Nut1.

-- Client Side Code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TransparencyEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("NutGiverTransparencyEvent")
local ImageLabel = script.Parent

TransparencyEvent.OnClientEvent:Connect(function()
   print("ImageLabel transparency before change: " .. ImageLabel.ImageTransparency)
   ImageLabel.ImageTransparency = 0
   print("ImageLabel transparency set to 0")
   print("ImageLabel transparency after change: " .. ImageLabel.ImageTransparency)
end)

1 comment

1

Thanks for this detailed answer! It worked perfectly

My last idea is to try moving the code that updates the ImageLabel's transparency to the client-side of your game, specifically in the "PlayerGui" object.

The issue might be because of the timing of the change in the transparency of the ImageLabel. When you die in the game, the entire screen reloads, including the UI. This might be why the change in transparency is taking effect only after you die in the game. Using the code below, the change in transparency can take effect immediately, without having to wait for the screen to reload.

local ImageLabel = game.StarterGui.Nuts.Nut1
local NutGiver = script.Parent

NutGiver.Touched:Connect(function(part)
    local player = game.Players:GetPlayerFromCharacter(part.Parent)
    if player then
        local playerGui = player.PlayerGui
        local ImageLabel2 = playerGui:WaitForChild("Nuts"):WaitForChild("Nut1")
        print("NutGiver touched by player: " .. player.Name)
        print("ImageLabel transparency before change: " .. ImageLabel2.ImageTransparency)
        ImageLabel2.ImageTransparency = 0
        print("ImageLabel transparency set to 0")
        print("ImageLabel transparency after change: " .. ImageLabel2.ImageTransparency)
    else
        print("NutGiver touched by non-player object")
    end
end)

5 comments

0

Sadly this didn't work. I did some testing and figured out that the transparency changes when I reset character though

0

Edited my answer - try that

0

This still didn't work. I don't think waiting is the answer because it prints the transparency as 0, even though the ImageLabel is not visually opaque until I reset character

0

Edited my answer with my last possible idea

0

It still is the same outcome unfortunately

After reviewing your script, I believe that the error is because you are attempting to edit the label directly from the StarterGui, which to my knowledge does not work properly. If you see that it changes the transparency from the server-side but not the client, this is surely the case. Rather than the variable you have defined as

local ImageLabel = game.StarterGui.Nuts.Nut1

I suggest changing that to

local ImageLabel = game.Players.LocalPlayer.PlayerGui.Nuts.Nut1

1 comment

0

This results in the code not running, nothing gets printed out and the ImageLabel doesn't change transparency even when I reset

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