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

This script isn't working outside of studio, help me please?

Asked by
Irvene 5
9 years ago

I'm getting very frustrated, this script isn't running and I'm in a bad mood someone can please help me out?

It's not running, it's a regular script inside of workspace. It's supposed to give every each player a GUI, but it's not doing so. It's very irritating. Please help me.

wait(3)

ScreenGui = Instance.new("ScreenGui"):Clone()
ScreenGui.Parent = game.ServerStorage
Background = Instance.new("Frame"):Clone()
Background.Parent = game.ServerStorage
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"):Clone()
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)
StarterMenu.Parent = game.ServerStorage

ImageText = Instance.new("ImageLabel"):Clone()
ImageText.Parent = game.ServerStorage
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,v in pairs(game.workspace:GetChildren()) do
    if v:findFirstChild("Humanoid") then
    plr = game.Players:GetPlayerFromCharacter(v)
    ScreenGui.Parent = plr.PlayerGui
    Background.Parent = plr.PlayerGui.ScreenGui
    Background:TweenSizeAndPosition(UDim2.new(0, 1000, 0, 1000), UDim2.new( 0, 0, 0, 0), "Out", "Quad", 1)
    wait(2)
    StarterMenu.Parent = plr.PlayerGui.ScreenGui
    StarterMenu.Visible = true
    ImageText.Parent = plr.PlayerGui.ScreenGui.ScrollingFrame
    wait()
else

    end

end



0
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. aquathorn321 858 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

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)--Runs whenever a player is added to the game
player.CharacterAdded:connect(function()--Runs whenever a character is added to the game. If you want to have the gui load only once when the character spawns, then leave out CharacterAdded and remove an end)

local ScreenGui = Instance.new("ScreenGui") --Parenting it is not necessary

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)--closes CharacterAdded() event
end)--closes PlayerAdded() event

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:

--Assuming your gui is pre-made
--Assuming Menu is in StarterGui
--Assuming the local script is in StarterGui
--Assuming Background is in ScreenGui
--Assuming StarterMenu is in ScreenGui
--Assuming ImageText in the 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
0
Once again it worked in studio mode, yet not inside of the actual game. Irvene 5 — 9y
0
It's because of your method. I'm fixing it right now. aquathorn321 858 — 9y
0
Edited my answer. aquathorn321 858 — 9y
0
There's a problem with your answer. Basically the problem is if it was in starterpack it'd run for every single player. So it'd run constantly, but if it was in workspace it'd be a gamescript running TO ALL players. Not every time a player joins. Irvene 5 — 9y
View all comments (4 more)
0
What do you mean? You mean the first one? The first one was just me fixing your initial errors, so you could see what you'd done wrong. The second was the solution in a server script, and the third was the solution in a local script. I recommend you use the second one. aquathorn321 858 — 9y
0
Have you even tried the scripts yet, or are you just inferring? aquathorn321 858 — 9y
0
I'm confused can you paste the one that has correct tweening, and positioning for the Image? Irvene 5 — 9y
0
Well I haven't done the tweening or positioning. This isn't a request site. I'm not even sure where you want the image. You can do that part on your own. Like I said: The second code block contains the solution for a normal script, and the third one contains the solution for a local script. aquathorn321 858 — 9y
Ad

Answer this question