Trying to give player a weapon when they press the "play" button

Like the title says, I want to give a player a weapon once they click play, so they dont have the gun while in the lobby.

Currently, when i press play nothing happens and you dont receive a weapon

here’s the code

local button = game.StarterGui.ScreenGui.Play
button.MouseButton1Click:Connect(function()
	game.Players.PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function()
			local backpack = player:WaitForChild("Backpack")
			local cloneTool = game.ServerStorage.M4:Clone()
			cloneTool.Parent = backpack
		end)
	end)	
end)

It’s a server script by the way

  1. You’re misusing the PlayerAdded and CharacterAdded functions, those only call when the player joins the experience or the player respawns.

  2. You need to use a client script that communicates with the server using a RemoteEvent.

You can learn about RemoteEvents here: Remote Functions and Events

If you’re still lost, basically you need two scripts, a local script in the button, and a server script preferably in ServerScriptService. A RemoteEvent needs to be placed in ReplicatedStorage, the only place that the server and client are truly the same.

When the button is pressed, the Remote shall be fired, which will make the server script do stuff.

You are using StarterGUI, so a click wont register anyways.

even if the click was registered, this would run for the next player that joins and not the player that clicked.

So, put the script under the button and use it like this:

local button = script.Parent
local player = script:FindFirstAncestorOfClass("Player")

button.MouseButton1Click:Connect(function()
	local backpack = player:WaitForChild("Backpack")
	local cloneTool = game.ServerStorage.M4:Clone()
	cloneTool.Parent = backpack
end)

Also, the more recommended way would be to use a remote event, a server script to handle the remote event, and a client script to handle the button. (This puts less stress on the server, even if only a bit less)

---server script
local re = --remote event

re.OnServerEvent:Connect(function(player)
	local backpack = player:WaitForChild("Backpack")
	local cloneTool = game.ServerStorage.M4:Clone()
	cloneTool.Parent = backpack
end)
--client script (under button)
local button = script.Parent
local player = game.Players.LocalPlayer
local re = --remote event

button.MouseButton1Click:Connect(function()
	re:FireServer()
end)

You’re checking for PlayerAdded after the button is clicked. The player will already be in, so the event won’t fire. The best way would be to use a local script and then fire a RemoteEvent to the server, which will pass over the player that fired it and give them the tools.

1 Like

Common mistake. This needs to be a local script and you need to use remote events which basically think of them like this: you escape the map, you tell the remote event that and then it tells the server to change the players escaped text. You need a local script because you need to get the player so you could get playergui. Everything in startergui gets cloned into playergui.

1 Like

Where is the script located?

Also, you are saying

Which means it will never even fire, because you are looking at StarterGui.

You want to look inside of the PlayerGui.

Hold on, why is the weapon in ServerStorage? Dosen’t that almost delete everything in serverstorage?

--Getting the player
game.Players.PlayerAdded:Connect(function(player)
	--Getting the character
	local character = player.Character or player.CharacterAdded
	local button = Player.PlayerGui.ScreenGui.Play
	button.MouseButton1Click:Connect(function()
		local backpack = player:FindFirstChild("Backpack")
		local cloneTool = game.ReplicatedStorage.Tools.M4:Clone()
		cloneTool.Parent = backpack
	end)
end)

Just do that instead

Local script (put under your button or wherever you want)

local button = script.Parent
local remote = game.ReplicatedStorage.GiveTool

button.Activated:Connect(function()
       remote:FireServer()
end)

Server script (put inside serverscriptservice)

local remote = game.ReplicatedStorage.GiveTool
local tool = where your tool is
remote.OnServerEvent:Connect(function(player)
       tool:Clone().Parent = player.Backpack
end)

What? What do you even mean? Serverstorage stores models for future reference which is used for loading maps. You can clone the thing in serverstorage and parent it to workspace and it’ll work.

He’s probably a novice and have no idea why is everything disappears in serverscriptservice and serverstorage when playtesting

1 Like

It’s in server storage so i can store it and use it later…

I just came back to this a few hours later, but some people are saying that it shouldn’t be in ServerStorage, that it would delete everything in ServerStorage, and that you are a novice for putting the model there, but ServerStorage is actually the correct place to put it. Putting it in ServerStorage simply means only the server can access the model, which in this case, doesn’t affect the code and is actually what is needed anyways as cloning a gun on the client usually doesn’t work. For the people saying everything in ServerStorage gets deleted, it may look like that on the client but you probably didn’t look on the server and check there. It’s called ServerStorage for a reason. Also, if it were to delete everything, what would be the point of ServerStorage even existing?

1 Like

There’s only 1 person saying that serverstorage will delete everything lmao, plus i’ve already sent the example code he need to use

There are two people including you.

Edit: nevermind, took that out of context and read it properly the third time. You were correcting him lmao.

Lmao what :skull:. I was saying that he has no idea why does everything disappears for player in serverv storage and serverscriptservice

I noticed :sweat_smile: I had edited that right after I sent it

2 Likes

I have no clue. That’s the only way to load and unload. I use it for my game because it’s kinda laggy so it switches between maps.

It still doesn’t delete the item like you said it did.

I wasn’t the one who said it didn’t i said it didn’t. I told you I don’t know because i don’t know what the person who said that means. It’s 1 person.