Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I want a gui button to give me a weapon?

so i tried this code to give me a weapon on press of the button and it didnt work

local player = game.Players.LocalPlayer -- Refer to the Player
local tool = game.ReplicatedStorage.Sword -- Refer to the Tool

script.Parent.MouseButton1Down:connect(function() -- When it's clicked
    if player:FindFirstChild("Backpack") then -- Not sure why I added this
        tool:Clone().Parent = player.Backpack -- Clone the tool to their inventory
    end -- End the if function
end) -- End the MouseButton1Down function 

2 comments

0

code block

0

alright, i showed an example

1 answer

You are cloning the tool with a LocalScript, but you can only clone with a server script, I believe. You'll have to use a RemoteEvent instead.

https://developer.roblox.com/en-us/api-reference/class/RemoteEvent

Example: (make sure you have a remoteevent inside of replicatedstorage)

button script:

local player = game.Players.LocalPlayer 

script.Parent.MouseButton1Down:connect(
    function()
        game.ReplicatedStorage.RemoteEvent:FireServer(player)
    end
)

backpack script inside of serverscriptservice:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(
    function(player)
        local tool = game.ReplicatedStorage.Sword
        tool:Clone().Parent = player.Backpack
    end
)

21 comments

0

damn my grammar

0

i've just read this and i have no idea what any of this means

0

yh lemme edit

0

not that the website

0

i could show an example code

0

can you? im not very good with code lol

0

sure

0

thanks

0

do i use a script or a local script for the server bit?

0

script

0

thx

0

how do i do it with a weapon name 'trenchgun'

0

just change local tool = game.ReplicatedStorage.Sword

0

also it didnt work

0

wait im so confused are my weapons supposed to be in replicatesstorage?

0

yes

0

...

0

im trying it now... im testing it

0

didnt work,

0

I FIXED IT BUT IT GIVES ME BOTH WEAPONS AT THE SAME TIME 0_0

0

and continues to give me them when i press repetitively

This discussion is preserved as it was. New answers, comments, and votes are closed.