You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve?
I want to figure out why in the lines of code provided below are resulting in the particle emitters being destroyed. -
What is the issue?
I am making a gun. There is the actual tool, and the viewmodel. The viewmodel is fine, the only problem is how the gun will delete all the particles within it half of the time whenever shot, and then of course I get an error about how the particles do not exist. Code inside the tool (local):
-- services
local runserv = game:GetService("RunService")
local rs = game:GetService("ReplicatedStorage")
local plrs = game:GetService("Players")
local uis = game:GetService("UserInputService")
local ts = game:GetService("TweenService")
local plrgui = plrs.LocalPlayer.PlayerGui
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
-- objects
local gun = script.Parent
local cam = workspace.CurrentCamera
local plr = plrs.LocalPlayer
local char = plr.CharacterAdded:wait()
local viewmodel = rs:WaitForChild("MainGame"):WaitForChild("Guns"):WaitForChild("Pistol") or cam.Pistol
local hum = char:WaitForChild("Humanoid") or char:FindFirstChild("Humanoid") or nil
local mouse = plr:GetMouse()
local animationController = viewmodel.AnimationController
local animator = animationController.Animator
local shootsound = viewmodel:WaitForChild("Shoot")
local front = viewmodel.Pistol.Front
local Events = rs:WaitForChild("MainGame"):WaitForChild("Events")
-- bool values
local shotcooldown = false
-- other
local cf = CFrame.new()
local config = require(rs:WaitForChild("MainGame"):WaitForChild("Guns"):WaitForChild("Pistol"):WaitForChild("Config"))
local ti = TweenInfo.new(0.05,Enum.EasingStyle.Linear,Enum.EasingDirection.Out, 0, false, 0)
viewmodel.Parent = rs.UnequippedGuns
--NumberVariables
local SelectedFramePos = UDim2.new(0.9,0,0.677,0)
local sinspeed = 2.5
local sinsize = 8
local cosspeed = 4
local cossize = 12
local recoilspeed = 5
local recoilsize = 15
local equipping = false
local boolequipped = false
local bulletsLeft = 10
local totalBullets = 10
-- Connections
local equipConnection
--positions (x,y,z)
local posx = 0 -- x value
local posy = 0 -- y value
local posz = 0 -- z value
local anims = {
["Hold"] = viewmodel.AnimationController.Animator:LoadAnimation(viewmodel.Animations.Hold),
["Shoot"] = animator:LoadAnimation(viewmodel.Animations.Shoot),
["CharacterHold"] = hum:LoadAnimation(viewmodel.CharacterAnimations.Hold),
["Reload"] = animator:LoadAnimation(viewmodel.Animations.Reload),
["Equip"] = animator:LoadAnimation(viewmodel.Animations.Equip),
["UnEquip"] = animator:LoadAnimation(viewmodel.Animations.UnEquip)
} -- Animation table
--functions
function PositionClientModel()
if hum.MoveDirection.Magnitude > 0 and not equipping then
local sin = math.sin(time() * sinspeed)/sinsize
local cos = math.cos(time() * cosspeed)/cossize
--cf = cf:Lerp(cam.CFrame*CFrame.new(config.Offset_From_Camera.X+sin, config.Offset_From_Camera.Y+cos,config.Offset_From_Camera.Z),.6)
cf = cf:Lerp(cam.CFrame*CFrame.new(1+sin,-1.65+cos,1.9),1)
else
cf = cf:Lerp(cam.CFrame*CFrame.new(1,-1.65,1.9),1)
end
viewmodel:SetPrimaryPartCFrame(cf) -- set clients model to the CFrame of the camera
end
local function equipped() -- creating equip function
if not gun and viewmodel then return end -- making sure gun and viewmodel exists
boolequipped = true
for q, part in pairs (gun:GetDescendants()) do-- looping through all things parented to gun
if part.ClassName == ("MeshPart") then-- checking if checked part is basepart or unionoperation (union)
part.Transparency = 1 -- changing that part to invisible if it is a basepart or union
end
end
for index, image in pairs (plrgui["Main UI"]:GetDescendants()) do
if image.ClassName == ("ImageLabel") then
if image.Name ~= ("GunPictureBackGround") and image.Name ~= ("PistolImage") then
image.ImageTransparency = 0.5
end
end
end
plrgui["Main UI"].BulletCount.TextTransparency = 0
plrgui["Main UI"].GunPictureBackGround.ImageTransparency = 0
plrgui["Main UI"].GunPictureBackGround.PistolImage.ImageTransparency = 0
for q, vm in pairs (viewmodel:GetChildren()) do
if vm.ClassName == ("MeshPart") or vm.ClassName == ("Part") then
vm.Transparency = 1
end
end
viewmodel.Parent = cam -- Parenting ViewModel to players client camera
equipConnection = runserv.RenderStepped:Connect(function()
PositionClientModel() -- if equipped, it will position the pistol client model every frame
end)
anims["Equip"]:Play()
viewmodel.Equip.TimePosition = 0.2
viewmodel.Equip:Play()
equipping = true
wait(0.1)
for q, vm in pairs (viewmodel:GetChildren()) do
if vm.ClassName == ("MeshPart") or vm.ClassName == ("Part") then
vm.Transparency = 0
end
end
wait(0.23)
equipping = false
anims["Hold"]:Play() -- plays holding animations
anims["CharacterHold"]:Play()
mouse.Icon = "http://www.roblox.com/asset/?id=8190310041"
end
local function unequipped() -- creating unequip function
if not gun and viewmodel then return end -- making sure gun exists
plrgui["Main UI"].BulletCount.TextTransparency = 1
plrgui["Main UI"].GunPictureBackGround.ImageTransparency = 0.5
plrgui["Main UI"].GunPictureBackGround.PistolImage.ImageTransparency = 0.5
boolequipped = false
anims["UnEquip"]:Play()
wait(0.2)
viewmodel.UnEquip:Play()
viewmodel.Parent = rs:WaitForChild("UnequippedGuns") -- moving gun to unequipped folderif hum ~= nil then
anims["CharacterHold"]:Stop()
end
local function shoot() -- creating recoil function
if boolequipped then
if gun.Front:FindFirstChild("Muzzle") then
for q, serverEffect in pairs (gun.Front:GetChildren()) do
if serverEffect.ClassName == ("ParticleEmitter") or serverEffect.ClassName == ("PointLight") then
serverEffect:Destroy()
end
end
end
bulletsLeft -= 1
plrgui["Main UI"].BulletCount.Text = (bulletsLeft.."│"..totalBullets)
if gun:FindFirstChild("Shoot") then
gun.Shoot:Destroy()
end
if not shotcooldown then
shotcooldown = true
if char:FindFirstChild(script.Parent.Name) then
anims["Shoot"]:Play()
shootsound:Play()
for q, effect2 in pairs (front:GetChildren()) do
if effect2.ClassName == ("ParticleEmitter") then
effect2:Emit(100)
end
end
front.MuzzleLight.Brightness = 5
local tween = ts:Create(front.MuzzleLight, ti, {Brightness = 0})
tween:Play()
end
game:GetService("ReplicatedStorage"):WaitForChild("MainGame"):WaitForChild("ServerGunEvents").Shoot:FireServer(gun.Front, gun.Name, gun)
if mouse.target ~= nil then
if mouse.target.Parent:FindFirstChild("Humanoid") then
if mouse.target.Name == ("Head") then
rs:WaitForChild("MainGame"):WaitForChild("Events"):WaitForChild("Hurt"):FireServer(mouse.Target, gun.Name, true, gun)
else
rs:WaitForChild("MainGame"):WaitForChild("Events"):WaitForChild("Hurt"):FireServer(mouse.Target, gun.Name, false, gun)
end
end
end
wait(config.Cooldown_Between_Shots)
shotcooldown = false
end
end
end
local function reload()
end
-- Events
mouse.Button1Down:Connect(shoot) -- when left mouse click is pressed, shoot function will play
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
anims.Reload:Play()
game.Workspace.Reload:Play()
end
if input.KeyCode == Enum.KeyCode.One then
if boolequipped ~= true then
equipped()
Events.Equip:FireServer(true, gun)
else
Events.Equip:FireServer(false, gun)
unequipped()
end
end
end)
(line 151 for shoot function)
Server Code (in serverscript service game operator):
local plrs = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(0.1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
rs:WaitForChild("MainGame"):WaitForChild("Events"):WaitForChild("Hurt").OnServerEvent:Connect(function(plr, hit, gunName, headshotBool, gun)
local IsGun = rs:WaitForChild("MainGame"):WaitForChild("Guns"):WaitForChild(gunName)
local gunModule
local Damage
if IsGun then
if gunName == ("Pistol") then
gunModule = require(IsGun.Config)
Damage = gunModule.Damage
if hit.Parent:FindFirstChild("Humanoid") then
if headshotBool then
hit.Parent:FindFirstChild("Humanoid"):TakeDamage(Damage * 2)
else
hit.Parent:FindFirstChild("Humanoid"):TakeDamage(Damage)
end
end
end
end
end)
game.ReplicatedStorage.MainGame.ServerGunEvents.Shoot.OnServerEvent:Connect(function(plr, front, gunName, gun)
for q, FX in pairs (front:GetChildren()) do
if FX.ClassName == ("ParticleEmitter") then
FX:Emit(100)
end
end
front.MuzzleLight.Brightness = 5
local tween = ts:Create(front.MuzzleLight, ti, {Brightness = 0})
tween:Play()
gun.Shoot:Play()
end)
game.ReplicatedStorage.MainGame.Events.Equip.OnServerEvent:Connect(function(plr, boolEquip, gun)
if boolEquip == true then
plr.Character.Humanoid:EquipTool(gun)
else
plr.Character.Humanoid:UnequipTools()
end
end)
(line 30 for shoot remote event listener nameless function)
-
What solutions have you tried so far?
I did Ctrl Shift F and search “MuzzleFlash” one of the particles I am having problems with, and it only came up in the two scripts I have been reading through for the past half an hour. There are no instances where I us :Destroy on any of the particles, and the part the particles are parented to is sticking to the gun just fine, not falling.
Like I said these two scripts are the only scripts the muzzleflash or any of the other particles are mentioned in.