So I got an error in the module script, I don’t know why, here is the error:
Here is the module script so far:
local MainModule = {}
function MainModule.Update(viewmodel, dt)
viewmodel.HumanoidRootPart.CFrame = workspace.Camera.CFrame
end
function MainModule.weldgun(gun)
local Main = gun.GunComponets.Handle
for i,v in ipairs(gun:GetDescendants()) do
if v:IsA("BasePart") and v ~= Main then
local newMotor = Instance.new("Motor6D")
newMotor.Name = v.Name
newMotor.Part0 = Main
newMotor.Part1 = v
newMotor.C0 = newMotor.Part0.CFrame:inverse() * newMotor.Part1.CFrame
newMotor.Parent = Main
end
end
end
function MainModule.equip(viewmodel, gun, hold)
local GunHandle = gun.GunComponets.Handle
local HRP_Motor6D = viewmodel:WaitForChild("HumanoidRootPart").Handle
gun.Parent = viewmodel
HRP_Motor6D.Part1 = GunHandle
local Hold = viewmodel.AnimationController:LoadAnimation(hold)
Hold:Play()
end
function MainModule.cast(gun,endposition,velocity)
local GunBarrel = gun.GunComponets.Barrel
local Bullet = Instance.new("Part")
Bullet.Size = Vector3.new(1,1,5)
Bullet.Anchored = true
Bullet.CanCollide = false
Bullet.Color = Color3.fromRGB(255,255,255)
Bullet.Material = Enum.Material.Neon
Bullet.Parent = workspace
Bullet.CFrame = CFrame.new(GunBarrel.Position, endposition)
local Loop
Loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
Bullet.CFrame *= CFrame.new(0,0 -velocity * dt)
if (Bullet.Position - GunBarrel.Position).magnitude > 1000 then
Bullet:Destroy()
Loop:Disconnect()
end
end)
end
return MainModule
I guess we live in very different time zones, but I fixed that issue, but the bullet doesn’t spawn at the current position of the barrel, I don’t know why, it doesn’t even spawn at the barrel, it just spawns in the middle of your screen.
Can you make sure that the barrel is in the correct position? If it spawns in the middle of the space that means the barrel is in an incorrect position.
A video would be much better for me to understand but for now all I could recommend is to print() the barrel’s position and the camera’s position (since you said the bullet were spawning in the middle of your screen). And also try doing this after the CFrame.new(barrel.Position, endposition): Bullet.Position = barrel.Position
Regarding about aiming in reloading, I already gave a statement about aiming here and I won’t be doing a reloading tutorial since it’s very simple.
Update: ok so mid way writing this you editted your post and it looks like the bullet is spawning at 0, 0, 0 (correct me if im wrong). but still try the possible solution above
Yeah I don’t know why it spawns at 0,0,0, but it just does. Also, I used 60 velocity, like you did in your gun, but it moves way faster than your gun. Also reloading is already kind of simple, so I could probably do it myself.
I did print the barrels position, it’s fine. I modified the module to set the bullets position to the barrel, it works, but after 5-7 seconds, the bullet disappears. The bullet’s position isn’t 0,0,0 anymore, even when the bullet doesn’t appear.
I think they’re spawning underground. When I go into first person, the bullets go away.
Also, if possible, could you provide an rbxm file of the module script for part 1?
When ingame could you check if the gun is welded properly, and also may I see the LocalHandler?
Unfortunately, No. I already provided the source code below. I don’t want people to gain nothing out of my tutorial (also another reason I don’t use code blocks). Sorry!
local GunModel = game.ReplicatedStorage:WaitForChild("SR01A1")
local AnimationsFolder = game.ReplicatedStorage:WaitForChild("SR01A1_ANIMS")
local ViewModel = game.ReplicatedStorage:WaitForChild("ViewModel")
local Mouse = game.Players.LocalPlayer:GetMouse()
local MainModule = require(game.ReplicatedStorage:WaitForChild("MainModule"))
ViewModel.Parent = workspace.Camera
MainModule.weldgun(GunModel)
game:GetService("RunService").RenderStepped:Connect(function(dt)
MainModule.Update(ViewModel, dt)
end)
MainModule.equip(ViewModel,GunModel,AnimationsFolder.SR01A1_ANIM)
local IsPlayerHoldingMouse
local CanFire = true
local FireDelay = 0.1
game:GetService("RunService").Heartbeat:Connect(function(dt)
if IsPlayerHoldingMouse then
if CanFire then
CanFire = false
MainModule.cast(GunModel, Mouse.Hit.Position, 5)
wait(FireDelay)
print(ViewModel.SR01A1.GunComponets.Barrel.Position)
CanFire = true
end
end
end)
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
IsPlayerHoldingMouse = true
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
IsPlayerHoldingMouse = false
end
end)
3: That’s alright, it’s just hard to see the module in a small image.
local MainModule = {}
function MainModule.Update(viewmodel, dt)
viewmodel.HumanoidRootPart.CFrame = workspace.Camera.CFrame
end
function MainModule.weldgun(gun)
local Main = gun.GunComponets.Handle
for i,v in ipairs(gun:GetDescendants()) do
if v:IsA("BasePart") and v ~= Main then
local newMotor = Instance.new("Motor6D")
newMotor.Name = v.Name
newMotor.Part0 = Main
newMotor.Part1 = v
newMotor.C0 = newMotor.Part0.CFrame:inverse() * newMotor.Part1.CFrame
newMotor.Parent = Main
end
end
end
function MainModule.equip(viewmodel, gun, hold)
local GunHandle = gun.GunComponets.Handle
local HRP_Motor6D = viewmodel:WaitForChild("HumanoidRootPart").Handle
gun.Parent = viewmodel
HRP_Motor6D.Part1 = GunHandle
local Hold = viewmodel.AnimationController:LoadAnimation(hold)
Hold:Play()
end
function MainModule.cast(gun,endposition,velocity)
local GunBarrel = gun.GunComponets:WaitForChild("Barrel")
local Bullet = Instance.new("Part")
Bullet.Size = Vector3.new(1,1,5)
Bullet.Anchored = true
Bullet.CanCollide = false
Bullet.Color = Color3.new(255,255,255)
Bullet.Material = Enum.Material.Neon
Bullet.Parent = workspace
Bullet.CFrame = CFrame.new(Bullet.Position, endposition)
Bullet.Position = GunBarrel.Position
print(Bullet.Position)
local Loop
Loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
Bullet.CFrame *= CFrame.new(0, 0, -velocity * (dt * 5))
if (Bullet.Position - GunBarrel.Position).magnitude > 1000 then
Bullet:Destroy()
Loop:Disconnect()
end
end)
end
return MainModule
Without Bullet.Position = GunBarrel.Position the bullet spawns at 0,0,0