I have a server script that creates a bullet for a gun, and then ejects it using bodyvelocity and look vector. The bullet keeps going in the same direction though, and i have no errors in the output. What could i be doing wrong?
local event = game.ReplicatedStorage.GunEvents.Bullet
local tool = script.Parent.Parent
function onEvent(player, shotsound, hit)
shotsound:Play()
local bullet = Instance.new("Part")
bullet.Shape = Enum.PartType.Block
bullet.Color = Color3.new(0.694118, 0.462745, 0)
bullet.Material = Enum.Material.Metal
bullet.Size = Vector3.new(.2,.2,.2)
bullet.Parent = script.Parent
local barrel = tool.Barrel
local barrel2 = barrel.Position
local newcframe = CFrame.new(barrel2, hit.p)
bullet.CFrame = newcframe
local Velocity = Instance.new("BodyVelocity")
Velocity.maxForce = Vector3.new(math.huge, math.huge, math.huge)
Velocity.Velocity = bullet.CFrame.LookVector * 100
Velocity.Parent = bullet
bullet.Touched:connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') and hit.Parent ~= player.Character then
local humanoid1 = hit.Parent:FindFirstChild('Humanoid')
humanoid1:TakeDamage(20)
bullet:Destroy()
end
end)
bullet.Touched:connect(function(hit)
if hit:IsA("Part") and hit.Parent ~= player.Character then
bullet:Destroy()
end
end)
end
event.OnServerEvent:Connect(onEvent)
yeah, once you make sure the local script is firing the right arguments (speaking of I would make the local script send the hit.Position and rename it something in the arguments so that the touched event doesn’t interfere. After you’ve done all that use
local tool = script.Parent.Parent
local event = game.ReplicatedStorage.GunEvents.Bullet
local event2 = game.ReplicatedStorage.GunEvents.Reload
local shotsound = script.Parent.Shot
local canshoot = true
local cooldown = script.Parent.Cooldown.Value
local player= game.Players.LocalPlayer
local ammo = script.Parent.Ammo.Value
local mouse = player:GetMouse()
local hit = mouse.Hit.Position
function onShot()
if ammo >= 1 then
if canshoot == true then
canshoot = false
event:FireServer(shotsound, hit)
script.Parent.MuzzleFlash.Enabled = true
wait(cooldown)
script.Parent.MuzzleFlash.Enabled = false
canshoot = true
ammo = ammo - 1
end
else
print("Reloading")
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char.Humanoid
local reload = script.Parent.Reload
local animator = humanoid:FindFirstChildOfClass("Animator")
local animationTrack = animator:LoadAnimation(reload)
wait(.3)
animationTrack:Play()
wait(1)
ammo = 15
end
end
tool.Activated:Connect(onShot)
You’re sending the variable Hit which you’ve defined at the top. You have to define hit in the shot script, the reason its sending the same position over and over is because hit is never updated. It’s sending where your mouse was when you spawned.