I am trying to make a script where if a part is clicked it will clone a GUI but only the person that clicked on the part can see the GUI and when they click it again it deletes the clone and it can't get spammed.
This is my script:
01 | -- Varibles |
02 | local BodycamHolder = script.Parent.BodycamHolder |
03 | local BodycamGUI = game.ServerStorage.GUIs.BodyCam |
04 | local EquippedStatus = false |
05 | local Player = game.Players.LocalPlayer |
06 |
07 | -- Script |
08 |
09 | if script.Parent.BodycamHolder.ClickDetector.MouseClick then |
10 | local BodycamClone = BodycamGUI:Clone() |
11 | if EquippedStatus = = false then |
12 | BodycamClone.Parent = Player.PlayerGui |
13 | EquippedStatus = true |
14 | else |
15 | if EquippedStatus = = true then |
16 | BodycamClone:Destroy() |
17 | end |
18 | end |
19 | end |
Can anyone help me fix it?
Problem
This script has a couple problems.
You can't get the LocalPlayer on the server.
You need to know how events work.
Solution
The MouseClick event gives us a parameter of the Player who clicked. The parameter is actually a Player instance.
Also, you should check the output it can tell you if you got any errors in your code. The output can be found by clicking on "View" located on the topbar and you'll see an Output button click it and it should pop up.
01 | -- Varibles |
02 | local BodycamHolder = script.Parent.BodycamHolder |
03 | local BodycamGUI = game.ServerStorage.GUIs.BodyCam |
04 |
05 | -- Script |
06 |
07 | script.Parent.BodycamHolder.ClickDetector.MouseClick:Connect( function (PlayerWhoClicked) |
08 | if not PlayerWhoClicked.PlayerGui:FindFirstChild(BodycamGUI.Name) then |
09 | BodycamGUI:Clone().Parent = Player.PlayerGui |
10 | else |
11 | PlayerWhoClicked.PlayerGui [ BodycamGUI.Name ] :Destroy() |
12 | end |
13 | end ) |