How to make gui to choose weapons?

Hello, i made a screen gui and inside has a textButton. when the player clicks the button an ak-47 is cloned from ReplicatedStorage and put into the players backpack using a local script inside the textButton. The problem is that the gun is scuffed when its added to the players backpack. It doesnt do any damage or make any sounds.

Here is the script inside the TextButton that clones and adds it to the players backpack…

local player = game.Players.LocalPlayer
local tool = game.ReplicatedStorage.Weapons[“AK-47”]

script.Parent.MouseButton1Click:Connect(function()
if player then
local clone = tool:Clone()
clone.Parent = player.Backpack
end
end)

3 Likes

There was an error in my code that wasnt suppose to be in this post. i just fixed it to the correct code

1 Like

To get the player to equip the tool you have to use the function Humanoid:EquipTool()

More information on how to use that here:

Perhaps there is a problem with the weapon itself? Does it still work if it’s inside of the Backpack upon game startup? If so it might be a problem relating to how the weapon sets itself up and it expects itself to just spawn inside the backpack on game startup.

That only caused more bugs. Now the weapon is invisible. The problem wasn’t really equipting the gun anyway. The problem is that my guns don’t do any damage if you shoot another player. They dont make any sounds either. But if i put them in starter pack instead of using the gui then they work fine so i know the guns arent the problem

The weapons work perfectly fine in my game. They are in starter pack. But i want to add a weapon selection gui instead of just having them in starter pack

The problem might be that the new weapon is being created by the client itself, and is thus not replicated to the Server. To fix this you’d need to tell the server to add the weapon to the player, rather then the client creating the clone.

I seen somewhere that using a remote event and a server script might work(it was something similar to my problem) But i dont know alot about scripting so i dont know how to do that

You didn’t capitalize ‘parent’ on line 4.
It should be:

script.Parent.MouseButton1Click:Connect(function()

Please use this article on RemoteEvents & RemoteFunctions to learn how to use them:

1 Like

ty i fixed it in my code but still wont work :frowning:

Your issue is that your gun doesn’t damage or play sounds, correct? This is most likely an issue with your gun and not so much your code to get a tool. It most likely has issues when being parented.

You might also want to look into RemoteEvents as @CodeJared suggested. If your gun is server sided (which I imagine it is), it’s most likely why it isn’t firing.

I can show you an example if I need to.

There nothing i can really do about that then. I made these guns using a gun script from a free model.

How can i tell if my gun is “server sided”?

I’m going to go ahead and assume your gun is server sided for sounds and firing. This probably means the server doesn’t recognize that the tool is in the player’s Backpack.

I’d assume this would work:
You will need a LocalScript, a Script and a RemoteEvent under your TextButton object.
LocalScript:

local player = game.Players.LocalPlayer
local tool = game.ReplicatedStorage.Weapons.ak47
local RemoteEvent = script.Parent.RemoteEvent

script.Parent.MouseButton1Click:Connect(function()
	if player then
		RemoteEvent:FireServer(tool) -- Fires RemoteEvent to the server.
	end
end)

Script

local RemoteEvent = script.Parent.RemoteEvent

function GiveTool(player, tool)
	if player then
		local clone = tool:Clone()
		clone.Parent = player.Backpack
	end
end

RemoteEvent.OnServerEvent:Connect(GiveTool) -- Runs function when the event has been fired
6 Likes

It should have RemoteEvents and the functions for them in the Script object inside the gun.

1 Like

Wow it works perfect now thank you so much :slight_smile:

the sound and now the damage works thank you

Hey, so you should not use a local Script, because of Filtering Enabled, you can use a object value for the selected guns, and then fire a remote event, and by server side give the player the tools to their backpack, or equip them by character.Humanoid:EquipTool(toolhere)

Using a LocalScript is necessary in order to get the MouseButton1Click event from the TextButton.