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

How to Clone a part by pressing a button?

Asked by 6 years ago

I have a part that is in Server Storage.

I want to script it to get cloned in front of the player when the player presses 'E'

I also want it to get destroyed when the player clicks on it.

How do I go about this?

0
Not a request site *-* User#20388 0 — 6y
0
-______- I cant ask for help on script that would be cool to implement in different kinds of games? CodeREVKids 5 — 6y
0
I don't think scripts can access the ServerStorage, so you would probably have to use a RemoteEvent to do so, and research on the UserInputService, and you can use the .Touched event for when the player clicks it. oSyM8V3N 429 — 6y

1 answer

Log in to vote
0
Answered by
Jo_Bot 67
6 years ago

Easy. Here is what I would do.

local ClonePart = game.ServerStorage.ClonePart
local Player = game.Players.LocalPlayer
local Character = Player.Character
local UIS = game:GetService("UserInputService")

UIS.InputBegan:connect(function(key)
    partIn = false
    if key.KeyCode = Enum.KeyCode.E and partIn == false then
        partIn = true
        newClone = ClonePart:Clone()
        newClone.Parent = game.Workspace
        neClone.CFrame = Character.HumanoidRootPart.CFrame + CFrame.new(0,0,5)
        clickDetector = Instance.new("ClickDetector")
        clickDetector.Parent = newClone
        if clickDetector.Clicked == true and partIn == true then
            newClone:Destroy()
            partIn = false
        end
    end
end)

Ok, first off, ClonePart will be the part in ServerStorage, whatever the name of it would be. Player is obvious, any scripter will be familiar with the second line. The UIS I put in is a variable for the UserInputService, which can detect User Input, obviously. Then the function will run if any input is sensed, which has the title "key", which can be changed to any name. Then a variable called partIn is set to false. This variable will prevent a user from repeatidly cloning the part in. Then the If statement detects if the user presses "e" and if partIn == false. It then clones the part and makes it's parent the Workspace, it also makes it's CFrame the character's plus 5 in the z Axis. A click detector is also added in the part to be able to detect if you press the part, and it is parented to newClone. Then the next If Statement checks if you have clicked the part and partIn == true. it then destroys the part and makes partIn = false. I might have messed up on this script. Wow, lol, I just wrote that from the top of my head, lol. Again, I mite hav hadz de gramer orr sintax eror(lol).

0
Thanks so much!!!! Going to try this out and report back! CodeREVKids 5 — 6y
Ad

Answer this question