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

How can my script not find my PlayerGui when its there from the start?

Asked by 7 years ago
Edited 7 years ago

I have a frame in a screenGui named "OreBar" in StarterGui, and when I try to access the frame with my script, I cannot find it. This is the error on line 4

It works in studio but NOT the game The script is not in screenGui, but it does access it.

The following script goes into workspace.ores:

function AddOre(player) --this goes in ore
    local ClickedOre = script.Parent
    local OreValue = ClickedOre.V.Value
    player.PlayerGui:WaitForChild("OreBar", 20) --NOT WORKING HELP
    local OreBar = player.PlayerGui.OreBar.Frame

    local CurrentOreSpot = nil
    for i=1,6,1 do
        if OreBar["Ore"..i].Text == "None" then
            CurrentOreSpot = OreBar["Ore"..i]
            break
        end
    end

    if CurrentOreSpot == nil then
        print("No more room!")
        return
    end

    ClickedOre.Position = Vector3.new(190, 100, -375)
    ClickedOre.Anchored = true
    CurrentOreSpot.Text = "This ore is $"..OreValue
    CurrentOreSpot.Ore.Value = ClickedOre

end

script.Parent.ClickDetector.MouseClick:connect(AddOre)

You can safely ignore ALL the CurrentOreSpot junk, it is just part of the things inside OreBar

Without the time parameter in line 4 I get error: Infinite yield possible for WaitForChild()

With it I get error after 10 seconds: cannot find OreBar in PlayerGui. It is there. Ive made sure while playing the game in studio. This is the only script of 3 total scripts that modifiy OreBar, another being a script inside OreBar, and another is the intro to the game. None modify Name property of OreBar.

0
Is it a local script? bigbenbennett 18 — 7y
0
No crazywireman 2 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

You say it's in screengui, if so it has to be a local script. Yet you have

The problem here is you need instead of:

script.Parent.ClickDetector.MouseClick:connect(AddOre)

which is meant for world objects in workspace. Gui's work differently which require MouseButton1Up, MouseButton1Down, MouseButton1Click, MouseButton2Down, MouseButton2Up, and MouseButton2Click, it should be:

script.Parent.MouseButton1Click:connect(AddOre)
0
It's in a script in a part, not in the screenGui crazywireman 2 — 7y
Ad

Answer this question