I made a gun system and I have a viewmodel for it but the gun doesn't shoot from the hole? (Unsolved)

I have a viewmodel for my gun and before I had it when I shot down the bullet would come from the gun and shoot downward as expected, but when I tried to shoot down with my new viewmodel
the bullet came from forward instead of from the gun, it looks really ugly
robloxapp-20221208-1819026.wmv (3.6 MB)

local TweenService = game:GetService("TweenService")

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local UserInputService = game:GetService("UserInputService")
local config = tool:WaitForChild("Configuration")
local remotes = tool:WaitForChild("Remotes")
local gunCursorIcon = "rbxassetid://409468479"
local normalIcon = ""
local camera = workspace.CurrentCamera
local head = player.Character:WaitForChild("Head")

local cameraTweenInfo = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

local goal1 = {FieldOfView = config:WaitForChild("IronsightFov").Value}
local goal2 = {FieldOfView = 70}

local tween1 = TweenService:Create(camera, cameraTweenInfo, goal1)
local tween2 = TweenService:Create(camera, cameraTweenInfo, goal2)

-- Remotes
local castray = remotes:WaitForChild("CastRay")
local takedamage = remotes:WaitForChild("TakeDamage")
local playsound = remotes:WaitForChild("PlaySound")
local playanimation = remotes:WaitForChild("PlayAnimation")

local reserveAmmo = config:WaitForChild("AmmoReserve").Value
local mag = config:WaitForChild("MagSize").Value
local currentmag = config:WaitForChild("MagSize").Value
local currentreserveammo = config:WaitForChild("AmmoReserve").Value

local gungui = script:WaitForChild("GunGui")

local fireable = true

local equipped = false


local function isFirstPerson()
	if (head.CFrame.p - camera.CFrame.p).Magnitude < 3 then
		return true
	else
		return false
	end
end

gungui:WaitForChild("Frame"):WaitForChild("AmmoInMag").Text = tostring(currentmag)
gungui:WaitForChild("Frame"):WaitForChild("AmmoInMagShadow").Text = tostring(currentmag)

tool.Equipped:Connect(function(mouse)
	
	mouse.Icon = gunCursorIcon
	equipped = true
	gungui.Parent = player:WaitForChild("PlayerGui")
	playanimation:FireServer(tool:WaitForChild("Animations"):WaitForChild("Idle"), tool:WaitForChild("Animations"):WaitForChild("Idle"):WaitForChild("Speed").Value)
	
	-- Reloading
	UserInputService.InputBegan:Connect(function(input, gameProccesed)
		if input.KeyCode == Enum.KeyCode.R and not gameProccesed and equipped then
			playanimation:FireServer(tool:WaitForChild("Animations"):WaitForChild("Reload"), tool:WaitForChild("Animations"):WaitForChild("Reload"):WaitForChild("Speed").Value)
			fireable = false
			tool:WaitForChild("Handle"):WaitForChild("Reload"):Play()
			wait(config.ReloadTime.Value)
			if currentreserveammo <= reserveAmmo then
				local addvalue = (mag - currentmag)
				if addvalue > currentreserveammo then
					gungui:WaitForChild("Frame"):WaitForChild("AmmoReserve").TextColor3 = Color3.fromRGB(255, 0, 0)
					currentmag = currentmag + currentreserveammo
					currentreserveammo = 0
					gungui:WaitForChild("Frame"):WaitForChild("AmmoInMag").Text = tostring(currentmag)
					gungui:WaitForChild("Frame"):WaitForChild("AmmoInMagShadow").Text = tostring(currentmag)
					gungui:WaitForChild("Frame"):WaitForChild("AmmoReserve").Text = "0"
					gungui:WaitForChild("Frame"):WaitForChild("AmmoReserveShadow").Text = "0"
				else
					currentmag = currentmag + addvalue
					currentreserveammo = currentreserveammo - addvalue
					gungui:WaitForChild("Frame"):WaitForChild("AmmoInMag").Text = tostring(currentmag)
					gungui:WaitForChild("Frame"):WaitForChild("AmmoInMagShadow").Text = tostring(currentmag)
					gungui:WaitForChild("Frame"):WaitForChild("AmmoReserve").Text = tostring(currentreserveammo)
					gungui:WaitForChild("Frame"):WaitForChild("AmmoReserveShadow").Text = tostring(currentreserveammo)
					gungui:WaitForChild("Frame"):WaitForChild("AmmoInMag").TextColor3 = Color3.fromRGB(255, 255, 255)
				end
			else
				local addvalue = (mag - currentmag)
				currentmag = currentmag + addvalue
				currentreserveammo = currentreserveammo - addvalue
				gungui:WaitForChild("Frame"):WaitForChild("AmmoInMag").Text = currentmag
				gungui:WaitForChild("Frame"):WaitForChild("AmmoInMagShadow").Text = currentmag
				gungui:WaitForChild("Frame"):WaitForChild("AmmoReserve").Text = currentreserveammo
				gungui:WaitForChild("Frame"):WaitForChild("AmmoReserveShadow").Text = currentreserveammo
			end
			fireable = true
		end
	end)
	-- Auto Reload
	local function autoReload()
		if equipped then
			playanimation:FireServer(tool:WaitForChild("Animations"):WaitForChild("Reload"), tool:WaitForChild("Animations"):WaitForChild("Reload"):WaitForChild("Speed").Value)
			fireable = false
			tool:WaitForChild("Handle"):WaitForChild("Reload"):Play()
			wait(config.ReloadTime.Value)
			if currentreserveammo <= reserveAmmo then
				local addvalue = (mag - currentmag)
				if addvalue > currentreserveammo then
					gungui:WaitForChild("Frame"):WaitForChild("AmmoReserve").TextColor3 = Color3.fromRGB(255, 0, 0)
					currentmag = currentmag + currentreserveammo
					currentreserveammo = 0
					gungui:WaitForChild("Frame"):WaitForChild("AmmoInMag").Text = tostring(currentmag)
					gungui:WaitForChild("Frame"):WaitForChild("AmmoInMagShadow").Text = tostring(currentmag)
					gungui:WaitForChild("Frame"):WaitForChild("AmmoReserve").Text = "0"
					gungui:WaitForChild("Frame"):WaitForChild("AmmoReserveShadow").Text = "0"
				else
					currentmag = currentmag + addvalue
					currentreserveammo = currentreserveammo - addvalue
					gungui:WaitForChild("Frame"):WaitForChild("AmmoInMag").Text = tostring(currentmag)
					gungui:WaitForChild("Frame"):WaitForChild("AmmoInMagShadow").Text = tostring(currentmag)
					gungui:WaitForChild("Frame"):WaitForChild("AmmoReserve").Text = tostring(currentreserveammo)
					gungui:WaitForChild("Frame"):WaitForChild("AmmoReserveShadow").Text = tostring(currentreserveammo)
					gungui:WaitForChild("Frame"):WaitForChild("AmmoInMag").TextColor3 = Color3.fromRGB(255, 255, 255)
				end
			else
				local addvalue = (mag - currentmag)
				currentmag = currentmag + addvalue
				currentreserveammo = currentreserveammo - addvalue
				gungui:WaitForChild("Frame"):WaitForChild("AmmoInMag").Text = currentmag
				gungui:WaitForChild("Frame"):WaitForChild("AmmoInMagShadow").Text = currentmag
				gungui:WaitForChild("Frame"):WaitForChild("AmmoReserve").Text = currentreserveammo
				gungui:WaitForChild("Frame"):WaitForChild("AmmoReserveShadow").Text = currentreserveammo
			end
			fireable = true
		end
	end
	
	-- Ironsight
	
	UserInputService.InputBegan:Connect(function(input, gameProccesed)
		if not gameProccesed and input.UserInputType == Enum.UserInputType.MouseButton2 or input.KeyCode == Enum.KeyCode.LeftControl then
			local firstPerson = isFirstPerson()
			
			if firstPerson and config:WaitForChild("Ironsight").Value then
				tween1:Play()
				if config:WaitForChild("IronsightWalkSpeedReduce").Value then
					humanoid.WalkSpeed = config:WaitForChild("IronsightWalkSpeed").Value
				end
			end
		end
	end)
	
	UserInputService.InputEnded:Connect(function(input, gameProccesed)
		if not gameProccesed or input.UserInputType == Enum.UserInputType.MouseButton2 or input.KeyCode == Enum.KeyCode.LeftControl then
			tween2:Play()
			
			if config:WaitForChild("IronsightWalkSpeedReduce").Value then
				humanoid.WalkSpeed = game:GetService("StarterPlayer").CharacterWalkSpeed
			end
		end
	end)
	
	-- Shooting
	UserInputService.InputBegan:Connect(function(input, gameProccesed)
		if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProccesed and equipped then
			
			local mousedown = false
			
			if config:WaitForChild("Automatic").Value then
				mousedown = true
			end
			
			UserInputService.InputEnded:Connect(function(input, gameProccesed)
				if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProccesed and equipped then
					mousedown = false
				end
			end)
			
			if fireable then
				repeat
					if currentmag > 0 then
						gungui:WaitForChild("Frame"):WaitForChild("AmmoInMag").TextColor3 = Color3.fromRGB(255, 255, 255)
						playanimation:FireServer(tool:WaitForChild("Animations"):WaitForChild("Recoil"), tool:WaitForChild("Animations"):WaitForChild("Recoil"):WaitForChild("Speed").Value)
						
						fireable = false
						local ray = Ray.new(tool:WaitForChild("Handle").CFrame.p, (mouse.Hit.p - tool:WaitForChild("Handle").CFrame.p).Unit * 2048)
						local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
						
						currentmag = currentmag - 1
						gungui:WaitForChild("Frame"):WaitForChild("AmmoInMag").Text = currentmag
						gungui:WaitForChild("Frame"):WaitForChild("AmmoInMagShadow").Text = currentmag
						
						castray:FireServer(ray, position)
						
						local beam = Instance.new("Part")
						beam.BrickColor = BrickColor.new("Deep orange")
						beam.FormFactor = Enum.FormFactor.Custom
						beam.Material = Enum.Material.Neon
						beam.Transparency = 1
						beam.Anchored = true
						beam.Locked = true
						beam.CanCollide = false
						beam.Parent = workspace
						
						local distance = (tool:WaitForChild("Handle").CFrame.p - position).Magnitude
						beam.Size = Vector3.new(0.3, 0.3, distance)
						beam.CFrame = CFrame.new(tool:WaitForChild("Handle").CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
						
						
						if part then
							local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
							
							if not humanoid then
								humanoid = part.Parent.Parent:FindFirstChildOfClass("Humanoid")
							end
							
							if humanoid then
								if part.Name == "Head" then
									takedamage:FireServer(humanoid, config:WaitForChild("Damage").Value * config:WaitForChild("HeadshotMultiplier").Value)
									script.Headshot:Play()
								else
									takedamage:FireServer(humanoid, config:WaitForChild("Damage").Value)
									script.HitMarker:Play()
								end
							end
						end
						
						playsound:FireServer(tool:WaitForChild("Handle"):WaitForChild("Fire"))
						tool:WaitForChild("Handle"):WaitForChild("Fire"):Play()
						wait(config:WaitForChild("Firerate").Value)
						fireable = true
						playanimation:FireServer(tool:WaitForChild("Animations"):WaitForChild("Idle"), tool:WaitForChild("Animations"):WaitForChild("Idle"):WaitForChild("Speed").Value)
						beam:Destroy()
					else
						gungui:WaitForChild("Frame"):WaitForChild("AmmoInMag").TextColor3 = Color3.fromRGB(255, 0, 0)
						tool:WaitForChild("Handle"):WaitForChild("Empty"):Play()
						
						if config:WaitForChild("AutoReload").Value then
							autoReload()
						end
						
						wait(config:WaitForChild("Firerate").Value)
					end
				until not mousedown
			end
		end
	end)
end)

tool.Unequipped:Connect(function()
	player:GetMouse().Icon = normalIcon
	equipped = false
	gungui.Parent = script
end)

playsound.OnClientEvent:Connect(function(sound)
	sound.Volume = 0
	script.Parent.Bolt.Transparency = 1
	script.Parent.Bolt2.Transparency = 0
	script.Parent.Hole.SpotLight.Enabled = true
	script.Parent.Hole.Smoke.Enabled = true
	script.Parent.Hole.Fire.Enabled = true
	script.Parent.Hole.Wind.Enabled = true
	wait(.01)
	script.Parent.B1.Transparency = 0
	script.Parent.Bolt3.Transparency = 0
	script.Parent.Bolt2.Transparency = 1
	wait(.02)
	script.Parent.B2.Transparency = 0
	script.Parent.B1.Transparency = 1
	wait(.01)
	script.Parent.B3.Transparency = 0
	script.Parent.B2.Transparency = 1
	script.Parent.Bolt3.Transparency = 1
	script.Parent.Bolt2.Transparency = 1
	script.Parent.Bolt.Transparency = 0
	script.Parent.Hole.Wind.Enabled = false
	script.Parent.Hole.Fire.Enabled = false
	script.Parent.Hole.Smoke.Enabled = false
	script.Parent.Hole.SpotLight.Enabled = false
	wait(.01)
	script.Parent.B3.Transparency = 1
	script.Parent.Handle.BDROP:Play()

end)
1 Like

Can you post a video or screenshot of the bullet being shot?

1 Like

no Idea why I need to because its just how I described, the bullet doesnt come out of the hole

its maybe because your hands are moving only on the client, so the server sees it in the same place so it shoots from where the server sees your hands and the weapon are.