I am working on the gun picking up and dropping system like L4D or Phantom Forces. My plan was to copy the gun model from ReplicatedStorage to ServerScriptService to wait for ammo statistics, then transmit to Workspace. Everything was going well until I spotted a problem 2 days ago.
At first, the gun was from ReplicatedStorage properly transmitted to Workspace when I give it the first try
However, when I pick up/drop the gun several times, the gun stopped appearing as the scripts become immobile to any inputs.
This is my main LocalScript:
gunPickup.OnClientEvent:Connect(function(weapon, value, ammo, storage)
reloading = false
reloadAnimTrack:Stop()
saveData()
if value == 1 then
gunDrop:FireServer(primary)
sendAmmo:FireServer(primary, priAmmo[1], priAmmo[2], character.HumanoidRootPart)
priAmmo[1] = ammo
priAmmo[2] = storage
primary = weapon
switch(primary)
elseif value == 2 then
gunDrop:FireServer(secondary) -- Transmit gun model from ReplicatedStorage to ServerScriptService to receive ammo statistics
sendAmmo:FireServer(secondary, secAmmo[1], secAmmo[2], character.HumanoidRootPart) -- Send ammo stats to the gun which is in ServerScriptService
secAmmo[1] = ammo
secAmmo[2] = storage
secondary = weapon
switch(secondary)
end
end)
Gun dropping from ServerScript:
local replicatedStorage = game:WaitForChild('ReplicatedStorage')
local event = replicatedStorage.Events:WaitForChild('GunDrop')
local models = replicatedStorage:WaitForChild('Collectibles')
local function GunDrop(player, weapon)
if weapon ~= nil then
print('Copied weapon to Server')
local droppedGun = models:WaitForChild(weapon.Name):Clone()
droppedGun.Parent = game:GetService('ServerScriptService')
return true
end
end
event.OnServerEvent:Connect(GunDrop)
ServerScript inside a gun to receive ammo information from main script (All guns have the same structure below, so I copied from a custom weapon):
local RS = game:GetService('ReplicatedStorage')
local GunPickup = RS.Events:WaitForChild('GunPickup')
local sendAmmo = RS.Events:WaitForChild('SendAmmo')
local gun = script.Parent
local prompt = gun.Handle.ProximityPrompt
local gunValue = 1
local currentAmmo = 30
local ammoStorage = 300
prompt.Triggered:Connect(function(player)
GunPickup:FireClient(player, gun, gunValue, currentAmmo, ammoStorage)
gun:Destroy()
end)
gun.AncestryChanged:Connect(function(gun, parent)
if parent == game:GetService('ServerScriptService') then
sendAmmo.OnServerEvent:Connect(function(player, gunName, ammo, storage, rootPart)
repeat wait() until gunName
if gun.Name == gunName.Name then
print("Weapon sent to Workspace")
currentAmmo = ammo
ammoStorage = storage
gun.Parent = workspace
gun:WaitForChild('Handle').CFrame = rootPart.CFrame * CFrame.new(0,1,-3) * CFrame.Angles(0, math.rad(90),0)
wait(1)
prompt.Enabled = true
end
end)
end
end)
So what have I been doing wrong and how can I fix it?
PS: If these given information is not enough, you can copy the game from here: