Hello! Hopefully someone will be able to help, but im having a little bit of a problem.
In the script below i get all the childs of the folder firearmsFolder and advancedToolsFolder so i can give them the functionality to fire, the problem is that, when i clone them to the players backpack with another script the weapon stops working completly; but if i take the weapon from ReplicatedStorage and i put it on my backpack then it works.
I tried to check when a child was added to the backpack but it just threw errors. I also tried searching on the forum, there were similar posts but none gave a solution to my problem
This is the script i have on ServerScriptService that gives all the weapons their functionalities
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local firearmsFolder = ReplicatedStorage.Items.Firearms
local advancedToolsFolder = ReplicatedStorage.AdvanceTools
local advancedId = 1507590113
for i,firearm in pairs(firearmsFolder:GetChildren()) do
local fireRate = firearm:FindFirstChild("FireRate").Value
local debounce = {}
local event = ReplicatedStorage.RemoteEvents.FireEvent
local handle = firearm:FindFirstChild("Handle")
local sounds = handle:FindFirstChild("Sounds")
firearm.Equipped:Connect(function()
task.wait(0.1)
sounds.Equip:Play()
end)
firearm.Unequipped:Connect(function()
sounds.Unequip:Play()
end)
firearm.Activated:Connect(function()
local player = Players:GetPlayerFromCharacter(firearm.Parent)
if player then
local currentTime = tick()
if debounce[player] and currentTime - debounce[player] < fireRate then
return
end
debounce[player] = currentTime
end
sounds.Activated:Play()
local shot = handle:FindFirstChild("Shot")
local dispenser = handle:FindFirstChild("Dispenser")
shot.GunShotLight.Enabled = true
shot.Transparency = 0
shot.Smoke1.Enabled = true
shot.Smoke2.Enabled = true
dispenser.s1.Enabled = true
dispenser.s2.Enabled = true
dispenser.s3.Enabled = true
dispenser.s4.Enabled = true
dispenser.s5.Enabled = true
dispenser.Decal1.Transparency = 0
dispenser.Decal2.Transparency = 0
task.wait(0.1)
shot.Transparency = 1
shot.GunShotLight.Enabled = false
shot.Smoke1.Enabled = false
shot.Smoke2.Enabled = false
dispenser.s1.Enabled = false
dispenser.s2.Enabled = false
dispenser.s3.Enabled = false
dispenser.s4.Enabled = false
dispenser.s5.Enabled = false
dispenser.Decal1.Transparency = 1
dispenser.Decal2.Transparency = 1
end)
Players.PlayerRemoving:Connect(function(plr)
debounce[plr] = nil
end)
event.OnServerEvent:Connect(function(plr, mousePosition)
local currentTime = tick()
if debounce[plr] and currentTime - debounce[plr] < fireRate then
return
end
debounce[plr] = currentTime
local raycastParameter = RaycastParams.new()
raycastParameter.FilterDescendantsInstances = {plr.Character}
raycastParameter.FilterType = Enum.RaycastFilterType.Exclude
local raycastParamResult = workspace:Raycast(firearm.Handle.Position, (mousePosition - firearm.Handle.Position) * 300, raycastParameter)
if raycastParamResult then
local hitPart = raycastParamResult.Instance
local model = hitPart:FindFirstAncestorOfClass("Model")
local canDamage = firearm:FindFirstChild("CanDamage")
if model then
if model:FindFirstChild("Humanoid") then
if canDamage.Value == false then
return
else
model.Humanoid.Health -= 30
end
end
end
end
end)
end
for i,firearm in pairs(advancedToolsFolder:GetChildren()) do
local fireRate = firearm:FindFirstChild("FireRate").Value
local debounce = {}
local event = ReplicatedStorage.RemoteEvents.FireEvent
local handle = firearm:FindFirstChild("Handle")
local sounds = handle:FindFirstChild("Sounds")
firearm.Equipped:Connect(function()
task.wait(0.1)
sounds.Equip:Play()
end)
firearm.Unequipped:Connect(function()
sounds.Unequipped:Play()
end)
firearm.Activated:Connect(function()
local player = Players:GetPlayerFromCharacter(firearm.Parent)
if not MarketplaceService:UserOwnsGamePassAsync(player.UserId, advancedId) then
player:Kick("MarketplaceService error! Report to dev team inmediatly!")
end
if player then
local currentTime = tick()
if debounce[player] and currentTime - debounce[player] < fireRate then
return
end
debounce[player] = currentTime
end
sounds.Activated:Play()
local shot = handle:FindFirstChild("Shot")
local dispenser = handle:FindFirstChild("Dispenser")
shot.GunShotLight.Enabled = true
shot.Transparency = 0
shot.Smoke1.Enabled = true
shot.Smoke2.Enabled = true
dispenser.s1.Enabled = true
dispenser.s2.Enabled = true
dispenser.s3.Enabled = true
dispenser.s4.Enabled = true
dispenser.s5.Enabled = true
dispenser.Decal1.Transparency = 0
dispenser.Decal2.Transparency = 0
task.wait(0.1)
shot.Transparency = 1
shot.GunShotLight.Enabled = false
shot.Smoke1.Enabled = false
shot.Smoke2.Enabled = false
dispenser.s1.Enabled = false
dispenser.s2.Enabled = false
dispenser.s3.Enabled = false
dispenser.s4.Enabled = false
dispenser.s5.Enabled = false
dispenser.Decal1.Transparency = 1
dispenser.Decal2.Transparency = 1
end)
Players.PlayerRemoving:Connect(function(plr)
debounce[plr] = nil
end)
event.OnServerEvent:Connect(function(plr, mousePosition)
local currentTime = tick()
if debounce[plr] and currentTime - debounce[plr] < fireRate then
return
end
debounce[plr] = currentTime
local raycastParameter = RaycastParams.new()
raycastParameter.FilterDescendantsInstances = {plr.Character}
raycastParameter.FilterType = Enum.RaycastFilterType.Exclude
local raycastParamResult = workspace:Raycast(firearm.Handle.Position, (mousePosition - firearm.Handle.Position) * 300, raycastParameter)
if raycastParamResult then
local hitPart = raycastParamResult.Instance
local model = hitPart:FindFirstAncestorOfClass("Model")
local canDamage = firearm:FindFirstChild("CanDamage")
if model then
if model:FindFirstChild("Humanoid") then
if canDamage.Value == false then
return
else
model.Humanoid.Health -= 30
end
end
end
end
end)
end
And here is the script that clones the weapons to the backpack when a event is fired, this script is also in ServerScriptService:
function addItem(plr,item,mod)
local cloneditem
local sameitem = plr.Backpack:FindFirstChild(item.Name) or plr.Character:FindFirstChild(item.Name)
if sameitem then
sameitem:Destroy()
local sameitemmodel = plr.Character:FindFirstChild(sameitem.Name.."Model")
if sameitemmodel then
sameitemmodel:Destroy()
end
else
cloneditem = item:Clone()
local newvalue = Instance.new("StringValue",cloneditem)
newvalue.Name = "OfficialTool"
if mod then
local newitemdata = script.Data:Clone()
newitemdata.Parent = cloneditem
require(newitemdata).ApplyData(mod)
end
end
if cloneditem then
if cloneditem:FindFirstChild("Slot") then
local slotvalue = plr.Character:FindFirstChild(cloneditem.Slot.Value)
if slotvalue then
local olditem = slotvalue.Value
if olditem then
olditem:Destroy()
local olditemmodel = plr.Character:FindFirstChild(olditem.Name.."Model")
if olditemmodel then
olditemmodel:Destroy()
end
end
slotvalue.Value = cloneditem
else
local newvalue = Instance.new("ObjectValue",plr.Character)
newvalue.Name = cloneditem.Slot.Value
newvalue.Value = cloneditem
end
end
cloneditem.Parent = plr.Backpack
if mod then
game.ReplicatedStorage.RemoteEvents.ApplyData:FireClient(plr,cloneditem.Data,mod)
end
end
end
game.ReplicatedStorage.RemoteEvents.Item.OnServerEvent:Connect(addItem)