Help solving some problems on a gun script

Its me again…
Well, u surely know that im making a gun a system, and im almost done with it! but i’ve got some problems and i would like if anyone could help me solve these problems

  • So, first problem is that
    At the first time the players shoots the position things dont work

  • Second Problem is that
    When the tweening thing ends, the player cant shoot anymore, none of the script works

  • And third problem is that
    Idk why but the take damage thing isnt working, how should i use it

Heres the script

local tool = script.Parent
local remote = tool:WaitForChild("OnShoot")
local shoot_part = tool:WaitForChild("FirePart")
local Config = tool:WaitForChild("Config")
local bullet = game.ServerStorage:WaitForChild("Shoot")
local TweenServ = game:GetService("TweenService")

local Shooting = false

tool.OnShoot.OnServerEvent:Connect(function(player, target)
	local targetpart = target
	if targetpart:IsA("BasePart") and targetpart.Parent:FindFirstChild("ZombieNoid") then
		if not Shooting then
			Shooting = true
			local hu = targetpart.Parent:FindFirstChild("HumanoidRootPart")
			if Config.Ammo.Value >= 0 then
				bullet:Clone().Parent = game.Workspace
				workspace.ChildAdded:Connect(function(newChild)
					if newChild.Name == "Shoot" then --some expected name
						newChild.Position = shoot_part.Position
						if newChild.Position ~= shoot_part.Position then
							newChild:Destroy()
						end
						local shoot = TweenServ:Create(newChild, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Position = targetpart.Position})
						shoot:Play()
						shoot.Completed:Connect(function()
							newChild.Position = Vector3.new(-999, -999, -999)
							targetpart.Parent.ZombieNoid:TakeDamage(Config.Damage.Value)
						end)
					end
				end)
			end
			wait(3)
			Shooting = false
		end
	end
end)
1 Like

Why are you using workspace.ChildAdded to get the cloned bullet object? That looks dodgy and might be causing you problems. Since the event connection is inside the “OnShoot” function, you’re also connecting to the event every time the weapon is shot, and never “disconnecting.”

You could just get the reference to the cloned bullet instead. Why not do something like this?

if Config.Ammo.Value > 0 then
	local newBullet = bullet:Clone()
	newBulet.Position = shoot_part.Position
	newBullet.Parent = workspace
	local shoot = TweenServ:Create(newBullet, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Position = targetpart.Position})
	shoot:Play()
	shoot.Completed:Wait() -- Yield until the tween is complete

	newBullet.Position = Vector3.new(-999, -999, -999) -- Why not just destroy the bullet?
	targetpart.Parent.ZombieNoid:TakeDamage(Config.Damage.Value)
end

In addition to this being a bit dodgy, I can see how this could directly relate to some of your existing problems (anticipating that there will be more with this approach!):

  • Problem 1. The first time the bullet is cloned (i.e. the first time the player shoots), the ChildAdded event isn’t “connected” to yet, so it won’t register.
  • Problem 2 and 3: see if this fixes it, otherwise there are other improvements we can make to the code.

*Unrelated but: I assume if Config.Ammo.Value >= 0 is meant to be if Config.Ammo.Value > 0. You probably don’t want the person to shoot if they have 0 ammo left!

It would also be helpful to mention if you’re getting any errors in the Output with this code.

There are a few other improvements I can point out with this code, but probably best to solve the main problems first.

ohhhhh ok ima try these later, also i didnt use destroy bc the bullets wouldnt spawn anymore, ima try these later and tell you

well, after the code changes it isnt working anymore, it doesnt shows anything in the output

Can you post the new full code with the adjustments?

Also clarify what you mean by it isn’t working - nothing is happening at all?