How would I convert this PlayerAdded gun giver, to a ClickDetector gungiver?

I need to convert this script from a PlayerAdded gun giver to a gun giver where you have to click on a brick. When I attempt to do this, the gun successfully goes into my inventory, but the scripts don’t load for some reason. I’m wondering if I’m making errors or not because my click detector script does not fully load in the scripts. Not sure if this is my scripting error or loading errors. Here is the fresh script that I need to convert:

local sss = script.Parent
local guns = {
sss[“AR-15”],
sss[“M4A1”],
}

local replicate = Instance.new(“RemoteEvent”,game.ReplicatedStorage)
replicate.Name = “Replicate”
local start = Instance.new(“RemoteEvent”,game.ReplicatedStorage)
start.Name = “Start”

game.Players.PlayerAdded:Connect(function( p )
p.CharacterAdded:connect(function( c )
script.Parent.Replicator:Clone().Parent = c
start:FireClient(p,nil)
for i,v in pairs(guns) do
local gun = v:Clone()
gun.Parent = p.Backpack
start:FireClient(p,gun.Objects)
gun.Global.Disabled = false
end
end)
end)

1 Like

Are you using a local script to give the player the gear?

please format your code by putting ` three times in front and back

like this

A ClickDetector has an event that fires when it is clicked, the event has an argument which is the player that clicked it.

ClickDetector.MouseClick:Connect(function(player)
    local c = player.Character
    --the rest of your code
end)
2 Likes

What @20amir02 said is how you should do it.

Here’s it in the context of your code in case you’re having trouble implementing it:

local sss = script.Parent
local guns = {
sss[“AR-15”],
sss[“M4A1”],
}

local replicate = Instance.new(“RemoteEvent”,game.ReplicatedStorage)
replicate.Name = “Replicate”
local start = Instance.new(“RemoteEvent”,game.ReplicatedStorage)
start.Name = “Start”

--Add this
local ClickDetector = game.Workspace.Part.ClickDetector --This should be the location of wherever the click detector instance is in the game

--Event changed to a click detector (the rest of the code, besides the events binding the function to a Player/Character Added, is unchanged)
ClickDetector.MouseClick:Connect(function(p)
	local c = p.Character
	script.Parent.Replicator:Clone().Parent = c
	start:FireClient(p,nil)
	for i,v in pairs(guns) do
		local gun = v:Clone()
		gun.Parent = p.Backpack
		start:FireClient(p,gun.Objects)
		gun.Global.Disabled = false
	end
end)

Yes, it perfectly works! But the problem is that the scripts inside of the gun don’t load in correctly. Here’s an example:

CLICK-DETECTOR TOOL GIVER:
https://gyazo.com/3a787d31aac5c3cb9d4af924c6e50e24

PLAYER-JOIN TOOL GIVER:
https://gyazo.com/d521027dd126cb0a847d04ac929e3cf2

As you can see, the click-detector tool giver does not load in the gui, and the other scripts. But the player-join tool giver loads in everything perfectly. Why is this? Is there a solution to this? I’m pretty sure that this is a loading problem.

Yes, I would agree - there’s some issue with the loading. This could be in regards to the script inside the guns or the script inside the GUI.

If you can’t figure out by just browsing through the code, feel free to add me on Discord and I would be happy to help you work through it!

Thanks! I need help, I can’t figure out what’s wrong with it.

This may be due to the scripts inside of your guns.

Maybe try adding a repeat wait() until Model.Parent.ClassName ~= WHEREVER

Some of your code may be running as soon as the game starts and not as soon as the player receives their gun. Also, have you tried checking Output or the Developer Console for any errors?

Yes, I’ve tried checking the console, no errors pop up.

I have a feeling that it may be due to the way your guns are coded - I know you may not want to share your gun code but maybe try looking through and seeing if the problem stems from there.