Strange beam positioning?

local TweenService = game:GetService("TweenService")
local SoundService = game:GetService("SoundService")
local Debris = game:GetService("Debris")

local Beam_Event = script.Parent
local Dark_Void = Beam_Event.Parent.Parent
local Cursed_Energy_Sound = Dark_Void:WaitForChild("Cursed_Energy")
local Beam = script:WaitForChild("Beam")

local MAX_RANGE = 256

local function Void_Sound(HRP)
	local Beam_Sound_Clone = Cursed_Energy_Sound:Clone()
	Beam_Sound_Clone.Parent = HRP
	Beam_Sound_Clone:Play()
	
	Debris:AddItem(Beam_Sound_Clone, 3)
end

local function Death(character)
	for i, v in character:GetDescendants() do
		if not v:IsA("Part") or v.Name == "HumanoidRootPart" then continue end
		v.Color = Color3.fromRGB(0, 0, 0)
	end
end

local function Beam_Hit(player, Origin, End_Position, Distance)
	local Beam_Clone = Beam:Clone()
	
	task.spawn(function()
		TweenService:Create(Beam_Clone, TweenInfo.new(0.1, Enum.EasingStyle.Linear), {Size = Vector3.new(20, 20, MAX_RANGE)}):Play()
	end)
	
	Beam_Clone.CFrame = CFrame.lookAt(Origin, End_Position) * CFrame.new(Vector3.new(0, 0, -Distance/2))
	Beam_Clone.Parent = game.Workspace
	
	Beam_Clone.Touched:Connect(function(hit)
		if not hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Name == player.Name then return end
		
		local Humanoid = hit.Parent:FindFirstChild("Humanoid")
		Humanoid:TakeDamage(100)
		
		Death(hit.Parent)
	end)
	
	local Disappear_Beam = TweenService:Create(Beam_Clone, TweenInfo.new(2, Enum.EasingStyle.Linear), {Transparency = 1})
	Disappear_Beam:Play()
	Disappear_Beam.Completed:Wait()
	
	Beam_Clone:Destroy()
end

local function Beam_Missed(player, Origin, Missed_End_Position)
	local Beam_Clone = Beam:Clone()
	
	task.spawn(function()
		TweenService:Create(Beam_Clone, TweenInfo.new(0.1, Enum.EasingStyle.Linear), {Size = Vector3.new(20, 20, MAX_RANGE)}):Play()
	end)
	
	Beam_Clone.CFrame = CFrame.lookAt(Origin, Missed_End_Position) * CFrame.new(Vector3.new(0, 0, -MAX_RANGE/2))
	Beam_Clone.Parent = game.Workspace
	
	Beam_Clone.Touched:Connect(function(hit)
		if not hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Name == player.Name then return end

		local Humanoid = hit.Parent:FindFirstChild("Humanoid")
		Humanoid:TakeDamage(100)
		
		Death(hit.Parent)
	end)
	
	local Disappear_Beam = TweenService:Create(Beam_Clone, TweenInfo.new(2, Enum.EasingStyle.Linear), {Transparency = 1})
	Disappear_Beam:Play()
	Disappear_Beam.Completed:Wait()

	Beam_Clone:Destroy()
end

Beam_Event.OnServerEvent:Connect(function(player, Origin, Direction, ExcludeList, HRP, Missed_End_Position)
	task.spawn(Void_Sound, HRP)
	
	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterType = Enum.RaycastFilterType.Exclude
	RaycastParameters.FilterDescendantsInstances = {ExcludeList}
	RaycastParameters.IgnoreWater = true
	
	local RayResults = game.Workspace:Raycast(Origin, Direction, RaycastParameters)
	
	if not RayResults then
		Beam_Missed(player, Origin, Missed_End_Position)
	else
		Beam_Hit(player, Origin, RayResults.Position, RayResults.Distance)
	end
end)



I attempted to make a beam when the tool is activated. The beam positioning when it misses postions properly but I don’t know why the beam’s position is completely off when it actually hits something.

Is there anyone who is able to help? :smiley:

Can you show a video of when it hits something?

1 Like

The script sets the position of the beam to half the distance but still tweens it to full size, so you just need to change this part

TweenService:Create(Beam_Clone, TweenInfo.new(0.1, Enum.EasingStyle.Linear), {Size = Vector3.new(20, 20, MAX_RANGE)}):Play()

to this

TweenService:Create(Beam_Clone, TweenInfo.new(0.1, Enum.EasingStyle.Linear), {Size = Vector3.new(20, 20, Distance)}):Play()
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.