So I'm developing a game where I have a gun tool and a gun model. The gun models spawn on transparent bricks across the map. I have disabled backpack and added a custom gui to house the guns you pick up. Basically the gun model has a click detector and a regular script that goes like this.
Gun = game.ServerStorage.Guns[script.Parent.Name]:Clone(); this = script.Parent; function onClicked(playerWhoClicked) local Backpacknum = playerWhoClicked.Backpack:GetChildren(); if (#Backpacknum >= 2)then playerWhoClicked.PlayerGui.ScreenGui.TMWError.Visible = true; return; end local Gun1var = playerWhoClicked.PlayerGui.ScreenGui.InventoryGui.Screen.Gun1; local Gun2var = playerWhoClicked.PlayerGui.ScreenGui.InventoryGui.Screen.Gun2; local Gun1dec = playerWhoClicked.PlayerGui.ScreenGui.InventoryGui.Screen.Gun1Dec; local Gun2dec = playerWhoClicked.PlayerGui.ScreenGui.InventoryGui.Screen.Gun2Dec; if (Gun1var.Value == "" and Gun2var.Value == "")then Gun1var.Value = script.Parent.Name; Gun1dec.Value = script.Parent.GunDecal.Value; elseif (Gun1var.Value ~= "" and Gun2var.Value == "") then Gun2var.Value = script.Parent.Name; Gun2dec.Value = script.Parent.GunDecal.Value; elseif (Gun2var.Value ~= "" and Gun1var.Value == "") then Gun1var.Value = script.Parent.Name; Gun1dec.Value = script.Parent.GunDecal.Value; end Gun.Parent = playerWhoClicked.Backpack; this:Destroy(); end script.Parent.ClickDetector.MouseClick:connect(onClicked)
This code is the script when you pick up or click the model of the weapon on the ground. Keep in mind this is a model and not a tool and this script is in all of the weapon models in the game. To help you understand the code above, in the gui there is two variables that stores the name of the gun model to display in the gui. Basically all this does is when you pick it up it determines what variables are empty and which one to put it in. This code works in the beginning when you pick up the model but when you drop the weapon and try to pick it back up essentially it doesn't launch the script inside the model.
Drop Weapon Code(Local Script):
GunPic1 = script.Parent.Gun1Dec; GunPic2 = script.Parent.Gun2Dec; GunImgLbl1 = script.Parent.GunImg1; GunImgLbl2 = script.Parent.GunImg2; GunLbl1 = script.Parent.GunLabel1; GunLbl2 = script.Parent.GunLabel2; DropBut1 = script.Parent.GunDrop1; DropBut2 = script.Parent.GunDrop2; Gun1 = script.Parent.Gun1; Gun2 = script.Parent.Gun2; Player = game.Players.LocalPlayer; GunAmmo1 = script.Parent.AmmoLabel1; GunAmmo2 = script.Parent.AmmoLabel2; Desc1 = script.Parent.Desc1; Desc2 = script.Parent.Desc2; DropBut1.MouseButton1Down:connect(function(open) if (Gun1.Value ~= "") then local FindWeapon = game.ReplicatedStorage.GunModel:FindFirstChild(Gun1.Value):Clone(); local BackPackWepFind = Player.Backpack:FindFirstChild(Gun1.Value); BackPackWepFind:Destroy(); GunPic1.Value = ""; GunImgLbl1.Image = ""; Gun1.Value = ""; Desc1.Text = ""; GunAmmo1.Text = ""; GunLbl1.Text = ""; FindWeapon.Parent = workspace; FindWeapon:MoveTo(Player.Character["Left Leg"].Position); end end) DropBut2.MouseButton1Down:connect(function(open) if (Gun2.Value ~= "") then local FindWeapon = game.ReplicatedStorage.GunModel:FindFirstChild(Gun2.Value):Clone(); local BackPackWepFind = Player.Backpack:FindFirstChild(Gun2.Value); BackPackWepFind:Destroy(); GunPic2.Value = ""; GunImgLbl2.Image = ""; Gun2.Value = ""; Desc2.Text = ""; GunAmmo2.Text = ""; GunLbl2.Text = ""; FindWeapon.Parent = workspace; FindWeapon:MoveTo(Player.Character["Right Leg"].Position); end end)
Basically on the gui that houses all the guns, are two drop buttons. When you click either drop button it basically looks at that variable that houses the gun names and finds the model with the click detector in the replicated storage and clones it and drops it at the characters legs. While it does this it deletes the tool of the gun in the players backpack aswell essentially dropping the gun. The drop function works just fine.
So on the studio mode this code all works just fine. I can pick up, drop, pick up again any other weapon, however in online play it doesn't work at all. However even in online play the M4A1 gun I have I'm able to pick it up, drop it, and pick it up again for some odd reason. Even though the code is exactly the same and is no special than any of the other guns.
If you made it this far I'd like to thank you for taking the time out to read this all. This bug is literally game breaking and I'm at the point where I can't fix it myself and need outside help desperately.
Try the following ~
When you first define "Player" make it
local Player = game:GetService('Players').LocalPlayer
When you first define "Character" do
local Character = Player.CharacterAdded:wait()