Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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!

4 comments

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.

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.

0

Makes sense. Thank you!

0

Np. :) Glad to help! :D

2 answers

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

3 comments

0

Thank you very much. I attempted the script, but now it is seeing the ClickDetector as a nil value. Still digging into though

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 :/

1

Odd. I'll tinker with it later. I did it in a rush.


``


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

1 comment

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?

This discussion is preserved as it was. New answers, comments, and votes are closed.