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

How do I make my click detector script work for gui enabling?

I put a click detector in a part. When I click the part I want a specific screen GUI to enable itself

My Script.

function Clicked(Plr)
print(Plr)
Plr.PlayerGui:WaitForChild("BookGui").ImageLabel.Visible =true
Plr.PlayerGui.BookGui.TextButton.Visible =true

end

the error I get is BookGui isn't a part of PlayerGui but it is! When I go in test mode its there.

2 answers

✓ Accepted answer

Errors: You can not access the PlayerGui because Gui's are set by the client. Maybe some Gui's created by the server you can access but most of the Gui's that we use is created with client.

You need to use RemoteEvents with FireClient

You can see RemoteEvents and FireClient on roblox wiki:

RemoteEvents/RemoteFunctions

FireClient

For this, you need to create a Remote Event, LocalScirpt, ClickDetector & Script

For first create a RemoteEvent and put it on Replicated Storage, now create a LocalScript and put it on StarterGui, on LocalScript located in StarterGui put this code:


-- FOR USE THIS PUT IN StarterGui --

repeat wait() until game.Players.LocalPlayer -- Not return nil for player

local event = game:GetService("ReplicatedStorage"):WaitForChild("Event") -- Put your event here

local plr = game.Players.LocalPlayer -- Get player

event.OnClientEvent:Connect(function() -- On fire the client
	
	local gui = "BookGui" -- Put your GUI Name here
	
	plr.PlayerGui:WaitForChild(tostring(gui)) -- Wait for GUI
	
	if plr["PlayerGui"]:FindFirstChild(tostring(gui)) then -- Find gui
		local gui = plr["PlayerGui"][tostring(gui)]
	    gui.ImageLabel.Visible = true -- set ImageLabel visibility to true
	    gui.TextButton.Visible = true -- Set TextButton visibility to true
	end
end)

Or you can put it on GUI and put this code:


-- FOR USE THIS PUT IN YOUR GUI --

wait()
local event = game:GetService("ReplicatedStorage"):WaitForChild("Event") -- Put your event here

event.OnClientEvent:Connect(function() -- On fire the client
	script.Parent.ImageLabel.Visible = true -- set ImageLabel visibility to true
	script.Parent.TextButton.Visible = true -- Set TextButton visibility to true
end)

Now create a Script and ClickDetector In your part and put this:

local event = game:GetService("ReplicatedStorage"):WaitForChild("Event") -- Put your event here

local clickdetector = script.Parent.ClickDetector -- Click detector location

clickdetector.MouseClick:Connect(function(plr)
	event:FireClient(plr)
end)

Hope it helped!

1 comment

0

Oh my god it works ! Thank you so much

You got that error, cuz you can't access to player's stuff from script(server script). If you want to do that, you need to use remote events. It is very easy.

For first insert RemoteEvent in ReplicatedStorage and change it name what you want. Now type this into ur clickdetector script

local Event = game:GetService("ReplicatedStorage"):WaitForChild("Your Event Here") --your remote event name here

function Clicked(player)
	Event:FireClient(plr) --giving exept player info that he clicked on the part
end

script.Parent.MouseClick:Connect(Clicked)

Now insert a localscript into startergui and type in

local Event = game:GetService("ReplicatedStorage"):WaitForChild("Your Event Here") --your remote event name here
local player = game.Players.LocalPlayer

Event.OnClientEvent:Connect(function() --this script starts when he get info from server
	player.PlayerGui:WaitForChild("BookGui").ImageLabel.Visible =true
	player.PlayerGui.BookGui.TextButton.Visible =true
end)

15 comments

0

The script isn't doing anything.. I don't get my gui popping up or an error in output.

0

then you did something wrong

0

Does the local script go inside the BookGui thats in startergui or just inside startergui

0

No, you can put it in bookgui

0

the local script inside the click detector has plr underlined where it says Event:FireClient(plr)

0

in clickdetector script do you have 7 line?

0

only 6

0

re-check and do what I said. Maybe you did somthing wrong

0

I tested it myself in its working

0

line 4 on clickdetector script is (plr) supposed to be underlined in orange

0
0

Ive written down the scripts myself rather then copying them and I filled in my events name in the place it says to but still no error in output and no gui showing

0

What if I take bookgui out of startergui and put it in the workspace and then use game.workspace to find the path?

0

Change a localscript in ClickDetector to normal

0

thanks for helping

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