Fixed code: (keep in mind this is just me cleaning up your initial script, do not take this as a solution. regard the second and third scripts as solutions)
wait(3)
ScreenGui = Instance.new("ScreenGui")
Background = Instance.new("Frame")
Background.ZIndex = 1
Background.Size = UDim2.new( 0, 0, 0, 0)
Background.Position = UDim2.new( 0, 0, 0, 0)
Background.Transparency = 0.5
Background.BackgroundColor3 = Color3.new(72/255,72/255,72/255)
StarterMenu = Instance.new("ScrollingFrame")
StarterMenu.ZIndex = 3
StarterMenu.Size = UDim2.new( 0, 250, 0, 150)
StarterMenu.Position = UDim2.new( 0.5, 0, 0.5, 0)
StarterMenu.Transparency = 0.5
StarterMenu.Visible = false
StarterMenu.BackgroundColor3 = Color3.new(85/255,0/255,0/255)
ImageText = Instance.new("ImageLabel")
ImageText.ZIndex = 3
ImageText.Image = "rbxassetid://51806623"
ImageText.Size = UDim2.new( 0, 100, 0, 100)
ImageText.Transparency = 1
ImageText.BackgroundColor3 = Color3.new(255/255,255/255,255/255)
for i,plr in pairs(game.Players:GetPlayers()) do
coroutine.resume(coroutine.create(function()
local ScreenGui = ScreenGui:Clone()
ScreenGui.Parent = plr.PlayerGui
local Background = Background:Clone()
Background.Parent = ScreenGui
Background:TweenSizeAndPosition(UDim2.new(0, 1000, 0, 1000), UDim2.new( 0, 0, 0, 0), "Out", "Quad", 1)
wait(2)
StarterMenu.Parent = ScreenGui
StarterMenu.Visible = true
ImageText.Parent = plr.PlayerGui.ScreenGui.ScrollingFrame
end))
end
Clone() creates a clone of the specified object.
It's like using Instance.new(), except it carries the properties of whatever it was cloned from.
You created an Instance and Cloned it in the same variable, which is the same as just creating the instance.
What you should do is clone the script for each player in the for loop.
Also, I'd define the clone locally, else you might have variable leaks or you might just move the same clone over and over into a player's PlayerGui.
In your for loop, you had a couple mistakes:
The cloning problem, obviously, and you iterated through workspace to find the character instead of just iterating through players using game.Players.GetPlayers, which returns a table of the Players in the game.
You checked to see if the character had a humanoid, and if so, plr would set to game.Players:GetPlayerFromCharacter(v).
The problem here is that if the loop were to come across a Model with a humanoid, take a zombie, for example, then it would set plr to nil and the loop would error.
You used wait() in a foor loop where you assert a code on each player.
Combined, this means it will take 3.03 seconds for the code in the loop to load for each player found.
We can solve this problem using coroutines, so that a new thread is created and the loop continues to run without hesitation.
Now, your main problem: **Why does the code only work in studio? **
It's because you're asserting only clone to each player, you're waiting only 3 seconds to give every existing player a gui, and your only running this once.
The client doesn't have enough time to load the character. The script is running before the player even has a character.
We can slove this problem by either waiting longer so that the players have time to load, or using the PlayerAdded() event.
The PlayerAdded() event calls a function whenever a Player joins the game.
It also creates a new thread and asserts the player as an argument to the specified function, so coroutines and loops are not necessary.
Additionally, if we wanted to give the player the gui whenever he/she spawns, we can use CharacterAdded(), which runs whenever the player's character spawns.
The CharacterAdded() asserts the character as an argument to the specified function.
Solution for Normal Script:
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function()
local ScreenGui = Instance.new("ScreenGui")
local Background = Instance.new("Frame")
Background.ZIndex = 1
Background.Size = UDim2.new( 0, 0, 0, 0)
Background.Position = UDim2.new( 0, 0, 0, 0)
Background.Transparency = 0.5
Background.BackgroundColor3 = Color3.new(72/255,72/255,72/255)
local StarterMenu = Instance.new("ScrollingFrame")
StarterMenu.ZIndex = 3
StarterMenu.Size = UDim2.new( 0, 250, 0, 150)
StarterMenu.Position = UDim2.new( 0.5, 0, 0.5, 0)
StarterMenu.Transparency = 0.5
StarterMenu.Visible = false
StarterMenu.BackgroundColor3 = Color3.new(85/255,0/255,0/255)
local ImageText = Instance.new("ImageLabel")
ImageText.ZIndex = 3
ImageText.Image = "rbxassetid://51806623"
ImageText.Size = UDim2.new( 0, 100, 0, 100)
ImageText.Transparency = 1
ImageText.BackgroundColor3 = Color3.new(255/255,255/255,255/255)
ScreenGui.Parent = player.PlayerGui
Background.Parent = ScreenGui
Background:TweenSizeAndPosition(UDim2.new(0, 1000, 0, 1000), UDim2.new( 0, 0, 0, 0), "Out", "Quad", 1)
wait(2)
StarterMenu.Parent = ScreenGui
StarterMenu.Visible = true
ImageText.Parent = ScreenGui
end)
end)
Alternatively, you could put it all in StarterGui and use a Local Script, which is a lot simpler. With this method, I suggest changing the name of ScreenGui to "Menu".
Don't use this method if you want the player to get the gui only when they enter the game and not whenever they spawn.
Solution for Local Script:
local ScreenGui = script.Parent:FindFirstChild("Menu")
ScreenGui.Background:TweenSizeAndPosition(UDim2.new(0, 1000, 0, 1000), UDim2.new( 0, 0, 0, 0), "Out", "Quad", 1)
wait(2)
ScreenGui.StarterMenu.Visible = true
script.ImageText.Parent = ScreenGui
1 comment
Whyare you cloning ScreenGui, Background, StarterMenu, and ImageText when you're creating them? I see your problem already: You don't know how to use clones.