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)
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:
Look over the documentation here.
You likely want to use Server -> Client communication for this. See this.
And a more detailed breakdown if you need it:
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)
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)
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