My purpose of this code is to delete the weapon in hand from the player and also the Starter Gear when a button is pressed. The code below is intended to just delete the tool from the hand. When I press the button nothing happens. How do I do this and also delete the weapon from starter gear?
game.Players.PlayerAdded:Connect(function(player) script.Parent.MouseButton1Click:Connect(function() local Character = player.Character or player.CharacterAdded:Wait() --local player = game.Players:GetPlayerFromCharacter(Character) local SG = player.StarterGear local tool = Character:FindFirstChildOfClass("Tool") --local name = tool.Name if tool then tool:Destroy() end end) end)
So something I noticed was that you are not referencing character correctly, it should be local tool = player.Character:FindFirstChildOfClass("tool"), here's the redone code for you
game.Players.PlayerAdded:Connect(function(player) script.Parent.MouseButton1Click:Connect(function() local SG = player.StarterGear local tool = player.Character:FindFirstChildOfClass("Tool") if tool.isA("Tool") then tool:Destroy() end end) end)
if SG:FindFirstChild(tool.Name) then SG[tool.Name]:Destroy() end
Here is why your button does not do anything when pressed
You cannot get MouseButton1Click from a serverscript
You could make a localscript that uses MouseButton1Click and when you press it you can fire a remote from client
here is an example
script.Parent.MouseButton1Click:Connect(function() game.ReplicatedStorage.remote:FireServer() end)