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

Click a part and opens a GUI,How???

I'm making a game that needs this. I made the script but somehow it doesn't work when I click it again.

Inside the part (w/ Click Detector) Script:

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	player:WaitForChild("PlayerGui").ScreenGui.Frame.Visible = true
end)

In Starter Gui: StarterGui, ScreenGui, Frame, TextButton, LocalScript

Inside the Local Script:

local player = game.Players.LocalPlayer
local target = game.Workspace.TeleportPart
script.Parent.MouseButton1Click:connect(function()
  player.Character.HumanoidRootPart.CFrame = target.CFrame * CFrame.new(0,3,0)
  player:WaitForChild("PlayerGui").ScreenGui.Frame.Visible = false
end)

When you click the "Part" again it doesn't work...Pls Help.

2 answers

✓ Accepted answer
local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

game:GetService("Players").PlayerAdded:Connect(function(Player)
	script.Parent.ClickDetector.MouseClick:Connect(function() -- you cannot use player as an argument because it is not the player that clicked the part, its the mouse. Therefore in this case, you need to learn a more advanced script called remote function. Go to ReplicatedStorage and insert a remoteEvent
		RemoteEvent:FireClient(Player)
	end)
end)

For the LocalScript:

local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer
local target = game.Workspace.TeleportPart

RemoteEvent.OnClientEvent:Connect(function() -- you fired a request from ServerScript to LocalScript using RemoteEvent. For more details on remote function, please check roblox devforum.
	player.Character.HumanoidRootPart.CFrame = target.CFrame * CFrame.new(0,3,0)
	script.Parent.Parent.Parent.Parent.Parent:WaitForChild("PlayerGui").ScreenGui.Frame.Visible = false -- script.Parent.Parent.Parent.Parent.Parent is the player him/herself. How i know this? Go into studio, then click players > playergui > screengui > frame > textbutton and you find your localscript. The localscript is given and placed in player's gui for every player when they join the game
end)

3 comments

0

I didn't know about Events and thank you for helping me!

0

no problem. here's roblox's guide about events. https://developer.roblox.com/en-us/api-reference/class/RemoteEvent. Btw you need RemoteEvent in this case because you cannot get the LocalPlayer via ServerScript directly unless you use functions like "PlayerAdded", "Part.Touched" etc. Therefore, because your case is "Clicked" function, you need a LocalScript

0

Also there is a problem with using "PlayerAdded" function to get the player. The "MouseClick" function can only be used once so once you clicked the part one time, it can't be clicked anymore. Thats why i suggest using RemoteEvent

Thank you so much guest_2018,but the LocalScript should be

local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local player = game.Players.LocalPlayer
local target = game.Workspace.TeleportPart

RemoteEvent.OnClientEvent:Connect(function()
    script.Parent.Parent.Parent.Parent.Parent:WaitForChild("PlayerGui").ScreenGui.Frame.Visible = true -- To make it visible when the Event is fired
end)

Inside the text button I added another LocalScript that teleports the player to another part

local player = game.Players.LocalPlayer
local target = game.Workspace.TeleportPart
script.Parent.MouseButton1Click:connect(function()
  player.Character.HumanoidRootPart.CFrame = target.CFrame * CFrame.new(0,3,0)
  player:WaitForChild("PlayerGui").ScreenGui.Frame.Visible = false
end)

thankfully it works!!

I didn't know about remote events and thank you for making me learn about it.

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