Hello, I’m trying to make a beam for my gun, but the beam goes in the wrong direction. I can send a video if needed, but just know it goes in the wrong direction
Script:
local Modules = game:GetService("ServerStorage"):WaitForChild("Server_Modules")
local GunSystem = require(Modules.GunSystem)
local Events = game:GetService("ReplicatedStorage"):WaitForChild("Events")
local ActivateEvent = Events:WaitForChild("GunEvent")
local debounce = {}
local function createBeam(handle, endPos, lifetime)
local attach0 = Instance.new("Attachment")
attach0.Position = handle:WaitForChild("Origin").Position
attach0.Parent = handle
local attach1 = Instance.new("Attachment")
attach1.WorldPosition = endPos
attach1.Parent = handle
local beam = Instance.new("Beam")
beam.Attachment0 = attach0
beam.Attachment1 = attach1
beam.FaceCamera = true
beam.Width0 = 0.1
beam.Width1 = 0.1
local debris = game:GetService("Debris")
--[[debris:AddItem(attach0, lifetime)
debris:AddItem(attach1, lifetime)
debris:AddItem(beam, lifetime)]]
beam.Parent = workspace.Effects
end
local function raycast(character, origin, direction, sound)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
if raycastResult then
if raycastResult.Instance:FindFirstAncestorWhichIsA("Model") then
local character = raycastResult.Instance:FindFirstAncestorWhichIsA("Model")
if character:FindFirstChild("Humanoid") then
character.Humanoid.Health = 0
--sound:Play()
end
end
return raycastResult.Position
else
return direction
end
end
local function onEvent(player, mousePos, origin, effects)
local character = player.Character
if not debounce[player] then
debounce[player] = true
local direction = (mousePos - origin).Unit * 100
local hit = GunSystem.wallcheck(character:WaitForChild("Head"))
if hit then
--origin = direction * -114
end
local endPos = raycast(character, origin, direction, effects["Sounds"].Death)
--ActivateEvent:FireAllClients(endPos, 0.1)
createBeam(effects.Parent.Parent:WaitForChild("Handle"), endPos, 5)
effects["Sounds"].Fire:Play()
task.wait(0.5)
debounce[player] = false
end
end
ActivateEvent.OnServerEvent:Connect(onEvent)
Thank you.