I am making a laser gun, but the projectile that it shoots does not actually go in the correct direction.
Here is my script.
-- Variables --
local Folder = game.Workspace.Projectiles
local Debris = game:GetService("Debris")
local Player = game.Players.PlayerAdded:Wait()
local Character = Player.CharacterAdded:Wait()
-- Main --
local function CreateBeam(Origin, Direction, Rotation)
local MidPoint = Origin + Direction/2
local Beam = game.ReplicatedStorage.Beam:Clone()
Beam.Parent = Folder
Beam.Anchored = true
Beam.CanCollide = false
Beam.CFrame = CFrame.new(MidPoint, Origin)
Beam.Size = Vector3.new(.5,.5, Direction.magnitude)
Debris:AddItem(Beam, 2)
end
game.ReplicatedStorage.Events.GunFireEvent.OnServerEvent:Connect(function(Player, MousePos, OriginPos)
local Direction = Character.Pistol.Handle.CFrame.LookVector
local Rotation = Character.Pistol.Handle.Origin.WorldPosition
local RayCast = workspace:Raycast(OriginPos, Direction)
if RayCast then
if RayCast.Instance.Name == "Head" then
local Character = RayCast.Instance.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
if Humanoid then
Humanoid:TakeDamage(95)
end
else
local Character = RayCast.Instance.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
if Humanoid then
Humanoid:TakeDamage(35)
end
end
if RayCast.Instance.Parent.Name == "Helmet" then
local Character = RayCast.Instance.Parent.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
if Humanoid then
Humanoid:TakeDamage(15)
end
end
if RayCast.Instance.Parent.Name == "FLC" then
local Character = RayCast.Instance.Parent.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
if Humanoid then
Humanoid:TakeDamage(5)
end
end
end
game.ReplicatedStorage.Events.PistolSoundEvent:FireAllClients()
CreateBeam(OriginPos, Direction, Rotation)
end)
Is there a way I can fix this?