Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

ImageLabels don't work in functions?

Asked by 6 years ago
game.Workspace.ChildAdded:connect(function(child)
local h = child:WaitForChild("Humanoid")
local Torso = h.Parent:FindFirstChild("Torso")
if h then
    local Smoke = Instance.new("Smoke")
    h.WalkSpeed = 32 --Change for different Walkspeed on join
    Smoke.Parent = Torso
    Smoke.Color = Color3.new(209, 0, 255)
    Smoke.Opacity = 0
    Smoke.RiseVelocity = 0
    Smoke.Size = 0
end

local StickyBombValue = Instance.new("IntValue")
StickyBombValue.Parent = child
StickyBombValue.Name = "StickyBombValue"

local ImageLabel = game.StarterGui.ScreenGui:FindFirstChild("PlayerImage")
ImageLabel.Parent = game.StarterGui.ScreenGui
ImageLabel.Position = UDim2.new(0, 25, 0, 50)
ImageLabel.Size = UDim2.new(0, 100, 0, 100)
ImageLabel.Image = "rbxassetid://133293265"
ImageLabel.BackgroundColor3 = Color3.new(207, 207, 207)

end)

This is a script from my game that is intended to set everything up when a player joins. You can ignore everything until the image label part. For some reason, the ImageLabel just won't do anything. It won't change position, change size and it's invisible. However, if I take that block of script out of game.Workspace.ChildAdded:Connect(function(child) and just put it at the bottom of the script then it works fine. Any ideas as to why the ImageLabel won't work if its in a function?

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Ok, so here are a few problems with your script - you don't use :ChildAdded when you want to do something when a player joins - you use :PlayerAdded, the second problem is that the contents inside of StarterGui get copied into the player when they join, so any modification made to anything inside of StarterGui after the player has joined WON'T modify the player's gui. The contents of StarterGui actually get copied inside of Player.PlayerGui (Player being whatever the player's name is), so a fix for that would be:

game.Players:PlayerAdded:connect(function(player) ~~~note the usage of PlayerAdded instead of ChildAdded
local h = player.Character:WaitForChild("Humanoid")
local Torso = h.Parent:FindFirstChild("Torso")
if h then
    local Smoke = Instance.new("Smoke")
    h.WalkSpeed = 32 --Change for different Walkspeed on join
    Smoke.Parent = Torso
    Smoke.Color = Color3.new(209, 0, 255)
    Smoke.Opacity = 0
    Smoke.RiseVelocity = 0
    Smoke.Size = 0
end

local StickyBombValue = Instance.new("IntValue")
StickyBombValue.Parent = child
StickyBombValue.Name = "StickyBombValue"

local ImageLabel = player.PlayerGui.ScreenGui:FindFirstChild("PlayerImage")
ImageLabel.Parent = game.StarterGui.ScreenGui
ImageLabel.Position = UDim2.new(0, 25, 0, 50)
ImageLabel.Size = UDim2.new(0, 100, 0, 100)
ImageLabel.Image = "rbxassetid://133293265"
ImageLabel.BackgroundColor3 = Color3.new(207, 207, 207)

end)

Additional explanations - the reason the code works after you put the part with the image label after the ChildAdded event block is because that code runs when the server starts, before the player/child added event is fired, so the image label is modified inside of StarterGui before the player joins, thus the modified contents of StarterGui get copied into the PlayerGui of the player.

~~~if I made any mistakes please point them out in the comments, thanks!~~~

0
Thank you for the detailed answer. I had no idea that StarterGuis were cloned into PlayerGuis. Your script worked too! I just needed to get the player guy to change its position, size, etc. to it. Thank you so much!!! tygerupercut3 68 — 6y
0
I meant to say get the player gui, not guy. Lol. tygerupercut3 68 — 6y
Ad

Answer this question