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

How to clone a M4 from serverstorage to a single players backpack by a textbutton?

Asked by 2 years ago

So if you press a TextButton, i want the weapon to move to the players backpack.

im new to scripting so this is my script:

if script.Parent.Parent.Equip.Activated then
    game.ServerStorage.M4A1:clone(game.Players.LocalPlayer.Backpack)
end

1 answer

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

It has to be a LocalScript(client) to detect input.

You can put this code in a LocalScript which the LocalScript has to be in workspace StarterGui, StarterPack, StarterCharacterScript, or StarterPlayerScripts.

local client = game.Players.LocalPlayer

local playerGui = client:WaitForChild("PlayerGui")

local screenGui = playerGui:WaitForChild("ScreenGui")
local textButton = screenGui:WaitForChild("TextButton")

textButton.MouseButton1Click:Connect(function()
   game.ServerStorage[M4A1]:Clone().Parent = client.Backpack
end)

----------EXPLANATION----------

First, I created 4 variables. The first variable is the client. The second variable is the PlayerGui which is the child of the client. The third is the ScreenGui. You've gotta think of StarterGui like a container when a player joins the server, the server will replicate its descendants and parent them to PlayerGui. The fourth is the TextButton, but if your TextButton is a descendant of the ScreenGui instance, you have to define the path to your TextButton.

After that, I'll add an event to the TextButton, which is the MouseButton1Click event. MouseButton1Click is an event that detects when the client left-clicks the GuiButton. So, after that, I'll connect the event to the function, which will run the following code. So, it'll clone the M4A1 and parent it to the Backpack. Why did I put the M4A1 into square brackets? Because of having numbers or spaces in the name of your instances, you'll have to put them into square brackets in your code.

Any questions or misinformation? Just reply to my answer

Ad

Answer this question