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

Opening a GUI from clicking an object. It works in Studio, but not in game. Why?

Asked by
Morficc 36
7 years ago

So I noticed today when I tested my game, that this function, that works in Studio, does not work. Any idea why? It is just a Script in Workspace, not a localscript.

wait(2)
local ResourceManager = require(game.ReplicatedStorage.ResourceManager)
local gui = game.Players.LocalPlayer.PlayerGui.PickupItemGui.ImageLabel

for _,v in next,game.Workspace.Items:GetChildren()do
v.ClickDetector.MouseClick:connect(function()
        local objectType = v.ObjectType.Value
        local data = ResourceManager.Resources[objectType]
        gui.TextLabel.Text = data.Name
        if gui.Visible == false then
        gui.Visible = true
        game.Players.LocalPlayer.PlayerGui.PickupItemGui.ImageLabel.ResourceType.Value = v.ObjectType.Value
        else
            end
        end)
end

Ty!

1
You can only use "LocalPlayer" in a localscript: the LocalPlayer is a client, and so the localscript return answer the client; think of it like in a business way: you, the employee, or local personal who answers calls, gets a call from a "client," and so you talk to & assist them; think of a localscript like that. TheeDeathCaster 2368 — 7y
1
And for an explanation for the server-side, think of it like a group of construction workers; you try to talk to them, but they can't talk b/c they're busy & they're not allowed to (until they're off-work); that's essentially a server-side in a nutshell. TheeDeathCaster 2368 — 7y
0
Makes sense. Thank you! Morficc 36 — 7y
0
Np. :) Glad to help! :D TheeDeathCaster 2368 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago

Because you can't call LocalPlayer using a regular Script, that has to be done with a LocalScript.

http://wiki.roblox.com/index.php?title=API:Class/Players/LocalPlayer http://wiki.roblox.com/index.php?title=API:Class/LocalScript http://wiki.roblox.com/index.php?title=API:Class/Script

Here's the verison that should be working properly:

Wait(2)
local ResourceManager = require(Game:GetService('ReplicatedStorage').ResourceManager)

for _, V in pairs, Game:GetService('Workspace').Items:GetChildren() do
V.ClickDetector.MouseClick:connect(function(Gui)
if Gui:FindFirstChild('PlayerGui') and Gui.PlayerGui:FindFirstChild('PickupItemGui') and Gui.PlayerGui.PickupItemGui:FindFirstChild('ImageLabeli') then
local Gui2 = Gui.PlayerGui.PickupItemGui.ImageLabel
local objectType = V.ObjectType.Value
local data = ResourceManager.Resources[objectType]
Gui2.TextLabel.Text = data.Name
if Gui2.Visible == false then
Gui2.Visible = true Gui2.ResourceType.Value = V.ObjectType.Value
else
end
end
end)
end
0
Thank you very much. I attempted the script, but now it is seeing the ClickDetector as a nil value. Still digging into though Morficc 36 — 7y
0
Well, i've tried about everything I can think of. I got the Click detector Nil message to go away, but the GUI still doesn't open :/ Morficc 36 — 7y
1
Odd. I'll tinker with it later. I did it in a rush. Naxxanar 77 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

``

local ResourceManager = ReplicatedStorage:WaitForChild("ResourceManager") local gui = game.Players.LocalPlayer.PlayerGui.PickupItemGui.ImageLabel wait (2) for _,v in next,game.Workspace.Items:GetChildren()do v.ClickDetector.MouseClick:connect(function() local objectType = v.ObjectType.Value local data = ResourceManager.Resources[objectType] gui.TextLabel.Text = data.Name if gui.Visible == false then gui.Visible = true game.Players.LocalPlayer.PlayerGui.PickupItemGui.ImageLabel.ResourceType.Value = v.ObjectType.Value else end end) end

0
Formatting was a little wonky there.. But I am not sure I understand. You changed where the wait(2) was, and took away the "Require" part of the ResourceManager call? Did I miss something else? Morficc 36 — 7y

Answer this question