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

How to clone a GUI when a Part is clicked? (Check Desc)

Asked by 2 years ago

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:

-- Varibles
local BodycamHolder = script.Parent.BodycamHolder
local BodycamGUI = game.ServerStorage.GUIs.BodyCam
local EquippedStatus = false
local Player = game.Players.LocalPlayer

-- Script

if script.Parent.BodycamHolder.ClickDetector.MouseClick then
    local BodycamClone = BodycamGUI:Clone()
    if EquippedStatus == false then
        BodycamClone.Parent = Player.PlayerGui
        EquippedStatus = true
    else
        if EquippedStatus == true then
            BodycamClone:Destroy()
        end
    end
end

Can anyone help me fix it?

0
Yeah, you should put your script in ServerScriptService if you want to hide it from exploiters. Still, even if it's in the workspace, exploiters can't see your code, but they can view and edit your LocalScripts' code and you can't do anything about it. This is the reason why you have to do all sanity checks on the server because the client can't be trusted. MarkedTomato 810 — 2y
0
It does not work. Joint_Ventures 5 — 1y

1 answer

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

Problem

This script has a couple problems.

  1. You can't get the LocalPlayer on the server.

  2. 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.

-- Varibles
local BodycamHolder = script.Parent.BodycamHolder
local BodycamGUI = game.ServerStorage.GUIs.BodyCam

-- Script

script.Parent.BodycamHolder.ClickDetector.MouseClick:Connect(function(PlayerWhoClicked)
    if not PlayerWhoClicked.PlayerGui:FindFirstChild(BodycamGUI.Name) then
        BodycamGUI:Clone().Parent = Player.PlayerGui
    else
         PlayerWhoClicked.PlayerGui[BodycamGUI.Name]:Destroy()
    end
end)
0
Hi! I have had my script in workspace, do I need to change it to ServerScriptService? Joint_Ventures 5 — 2y
Ad

Answer this question