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
8 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.

01wait(2)
02local ResourceManager = require(game.ReplicatedStorage.ResourceManager)
03local gui = game.Players.LocalPlayer.PlayerGui.PickupItemGui.ImageLabel
04 
05for _,v in next,game.Workspace.Items:GetChildren()do
06v.ClickDetector.MouseClick:connect(function()
07        local objectType = v.ObjectType.Value
08        local data = ResourceManager.Resources[objectType]
09        gui.TextLabel.Text = data.Name
10        if gui.Visible == false then
11        gui.Visible = true
12        game.Players.LocalPlayer.PlayerGui.PickupItemGui.ImageLabel.ResourceType.Value = v.ObjectType.Value
13        else
14            end
15        end)
16end

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 — 8y
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 — 8y
0
Makes sense. Thank you! Morficc 36 — 8y
0
Np. :) Glad to help! :D TheeDeathCaster 2368 — 8y

2 answers

Log in to vote
1
Answered by 8 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:

01Wait(2)
02local ResourceManager = require(Game:GetService('ReplicatedStorage').ResourceManager)
03 
04for _, V in pairs, Game:GetService('Workspace').Items:GetChildren() do
05V.ClickDetector.MouseClick:connect(function(Gui)
06if Gui:FindFirstChild('PlayerGui') and Gui.PlayerGui:FindFirstChild('PickupItemGui') and Gui.PlayerGui.PickupItemGui:FindFirstChild('ImageLabeli') then
07local Gui2 = Gui.PlayerGui.PickupItemGui.ImageLabel
08local objectType = V.ObjectType.Value
09local data = ResourceManager.Resources[objectType]
10Gui2.TextLabel.Text = data.Name
11if Gui2.Visible == false then
12Gui2.Visible = true Gui2.ResourceType.Value = V.ObjectType.Value
13else
14end
15end
16end)
17end
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 — 8y
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 — 8y
1
Odd. I'll tinker with it later. I did it in a rush. Naxxanar 77 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago
1<pre class="brush: lua">``
2</pre>

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 — 8y

Answer this question