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

How do I make UI appear when a part is clicked?

Asked by 6 years ago

Hello, I am currently trying to make ScreenGui appear in a Player's PlayerGui. I am having a difficult time trying to do this, someone please help!

local C1 = script.Parent.Parent.Chassis.ClickDetector


function Clicked(n)
    local character = game.Players.LocalPlayer
    local UI = game.ServerStorage.UnitUI:clone()
    local player = n:GetPlayerFromCharacter()
    UI.Parent = player.PlayerGui
end

C1.MouseClick:connect(Clicked)

UnitUI is a ScreenGui with a TextBox and other random stuff in it, which is all in ServerStorage. This is all in a LocalScript.

A response is appreciated!

0
LocalScripts can't access the descendants of ServerStorage, hence the name 'Server Storage' Rare_tendo 3000 — 6y

1 answer

Log in to vote
1
Answered by
nilVector 812 Moderation Voter
6 years ago

As far as I know, listening to the MouseClick event of a ClickDetector should be done from a server script, because the playerWhoClicked is passed in as an argument of the RBXScriptSignal to allow the developer to know which player had clicked the ClickDetector.

Knowing this, the n in your Clicked(n) function definition is the player who clicked. Let's rename it to something more informative, such as playerWhoClicked. Remember, we want this event to be handled in a server script, so you'll have to make some simple changes that would go something like this:

local C1 = script.Parent.Parent.Chassis.ClickDetector

function Clicked(playerWhoClicked)
    local UI = game.ServerStorage.UnitUI:clone()
    UI.Parent = playerWhoClicked.PlayerGui
end

C1.MouseClick:connect(Clicked)

(another minor mistake I found is that you believed that game.Players.LocalPlayer returns a character, when in fact, it returns the actual Player object - but that is unnecessary for this script since it is a server script. Just letting you know for future reference!)

0
Sooo you do not need the local character = game.Players.LocalPlayer line? cooldeath49 37 — 6y
0
Wow, thank you! cooldeath49 37 — 6y
Ad

Answer this question