I’m trying to create a wand tool that shoots beams which follow the direction of the user’s mouse. At the moment, the beam either goes in a specific direction or doesn’t follow the mouse and stays at one point.
Code:
-- Server Script
local wand = script.Parent
local handle = wand.Handle
local replicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = replicatedStorage.Wand.RemoteEvent
local equipped = false
local beamMode = true
local charging = false
local mousePosition
-- When wand is equipped
wand.Equipped:Connect(function()
equipped = true
end)
-- When wand is unequipped
wand.Unequipped:Connect(function()
charging = false
equipped = false
end)
-- Retrieves position of mouse from client
remoteEvent.OnServerEvent:Connect(function(player, mousePos)
mousePosition = mousePos
end)
-- When wand is clicked with left click
wand.Activated:Connect(function()
local magic = wand:FindFirstChild('Magic')
if not magic then
magic = wand.MagicToClone:Clone()
magic.Name = 'Magic'
magic.Parent = wand
magic.Start.Anchored = false
magic.Start.Position = wand.Point.Position
local weldConstraint = Instance.new('WeldConstraint')
weldConstraint.Parent = handle
weldConstraint.Part0 = handle
print(magic.Start)
weldConstraint.Part1 = magic.Start
end
if equipped then
if beamMode then
if not charging then
charging = true
print('Charging...')
for _, object in magic.Start:GetChildren() do
if object:IsA('ParticleEmitter') then
object.Transparency = NumberSequence.new(0)
-- Charging animation
end
end
else
charging = false
print('Released!')
-- Release Animation
for _, object in magic:GetChildren() do
for _, obj in object:GetChildren() do
if obj:IsA('Beam') or obj:IsA('ParticleEmitter') then
obj.Transparency = NumberSequence.new(0)
end
end
end
magic.End.Position = magic.Start.Position
local amount = 1000
while (pcall(addPosition, magic, amount)) do
addPosition(magic, amount)
task.wait(1)
end
end
else
print('not beammode')
end
end
end)
-- When mouse left click is released, it destroys cloned object called Magic
wand.Deactivated:Connect(function()
if not charging then
local magic = wand:FindFirstChild('Magic')
magic:Destroy()
end
end)
-- Part I keep changing and main part
function addPosition(magic, amount)
--magic.End.Position += Vector3.new(0, 0, amount)
magic.End.CFrame = magic.End.CFrame + mousePosition.CFrame.LookVector * amount
--magic.End.CFrame += mousePosition.CFrame.LookVector * amount
end
-- Localscript
local player = game:GetService('Players').LocalPlayer
local replicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = replicatedStorage.Wand.RemoteEvent
local num = 0
while true do
local mouse = player:GetMouse()
remoteEvent:FireServer(mouse.Hit.Position)
--remoteEvent:FireServer(mouse)
num += 1
--print('Loop ' .. num)
task.wait(1)
end
Image 1:
Video 1: Beam going in a specific direction
Video 2: Beam staying at one point, not moving and not following the mouse
-- Main part here
-- Video 1
magic.End.Position += Vector3.new(0, 0, amount)
-- Video 2
magic.End.CFrame = magic.End.CFrame + mousePosition.CFrame.LookVector * amount
I don’t know what I am doing wrong, the wand does the same thing
Video:
local amount = 1000
while (pcall(addPosition, magic, amount)) do
addPosition(magic, amount)
task.wait(1)
end
--
function addPosition(magic, amount)
magic.End.CFrame = mousePosition.CFrame.LookVector * amount
-- magic.End.CFrame += mousePosition.CFrame.LookVector * amount <- is it meant to be this? or the one above?
end)
-- Server Script
local wand = script.Parent
local handle = wand.Handle
local replicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = replicatedStorage.Wand.RemoteEvent
local equipped = false
local beamMode = true
local charging = false
local mousePosition
-- When wand is equipped
wand.Equipped:Connect(function()
equipped = true
end)
-- When wand is unequipped
wand.Unequipped:Connect(function()
charging = false
equipped = false
end)
-- Retrieves position of mouse from client
remoteEvent.OnServerEvent:Connect(function(player, mousePos)
mousePosition = mousePos
end)
-- When wand is clicked with left click
wand.Activated:Connect(function()
local magic = wand:FindFirstChild('Magic')
if not magic then
magic = wand.MagicToClone:Clone()
magic.Name = 'Magic'
magic.Parent = wand
magic.Start.Anchored = false
magic.Start.Position = wand.Point.Position
local weldConstraint = Instance.new('WeldConstraint')
weldConstraint.Parent = handle
weldConstraint.Part0 = handle
print(magic.Start)
weldConstraint.Part1 = magic.Start
end
if equipped then
if beamMode then
if not charging then
charging = true
print('Charging...')
for _, object in magic.Start:GetChildren() do
if object:IsA('ParticleEmitter') then
object.Transparency = NumberSequence.new(0)
-- Charging animation
end
end
else
charging = false
print('Released!')
-- Release Animation
for _, object in magic:GetChildren() do
for _, obj in object:GetChildren() do
if obj:IsA('Beam') or obj:IsA('ParticleEmitter') then
obj.Transparency = NumberSequence.new(0)
end
end
end
magic.End.Position = magic.Start.Position
local amount = 1000
local direction = (mousePosition.Position - magic.Start.Position).Unit
while (pcall(addPosition, magic, direction, amount)) do
addPosition(magic, direction, amount)
task.wait(1)
end
end
else
print('not beammode')
end
end
end)
-- When mouse left click is released, it destroys cloned object called Magic
wand.Deactivated:Connect(function()
if not charging then
local magic = wand:FindFirstChild('Magic')
magic:Destroy()
end
end)
-- Part I keep changing and main part
function addPosition(magic, direction, amount)
magic.End.CFrame = magic.End.CFrame + direction * amount
end
-- Localscript
local player = game:GetService('Players').LocalPlayer
local replicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = replicatedStorage.Wand.RemoteEvent
local num = 0
while true do
local mouse = player:GetMouse()
remoteEvent:FireServer(mouse)
num += 1
--print('Loop ' .. num)
task.wait(1)
end
Server Script, it shoots the beam which it has never done before but I think I have to fix the mousePosition part, because it gets the position when it charges the wand instead of during when it is released.
How to stop the beam from going through objects like walls, this is what I have so far. Also is there a way to make it so you can make it change the direction instead of it going in a fixed direction of the original mousePosition.
Video 1:
local continueRunning = true
local endPoint = magic.End
local direction = (mousePosition - magic.Start.Position).Unit
local amount = 1
endPoint.Touched:Connect(function(hit)
if not hit:IsDescendantOf(wand) then
continueRunning = false
end
end)
while continueRunning do
addPosition(endPoint, direction, amount)
if not pcall(addPosition, endPoint, direction, amount) then
continueRunning = false
end
task.wait(0.1)
end
-- Server Script
local wand = script.Parent
local handle = wand.Handle
local replicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = replicatedStorage.Wand.RemoteEvent
local equipped = false
local beamMode = true
local charging = false
local mousePosition
-- When wand is equipped
wand.Equipped:Connect(function()
equipped = true
end)
-- When wand is unequipped
wand.Unequipped:Connect(function()
charging = false
equipped = false
end)
-- Retrieves position of mouse from client
remoteEvent.OnServerEvent:Connect(function(player, mousePos)
mousePosition = mousePos
end)
-- When wand is clicked with left click
wand.Activated:Connect(function()
local magic = wand:FindFirstChild('Magic')
if not magic then
magic = wand.MagicToClone:Clone()
magic.Name = 'Magic'
magic.Parent = wand
magic.Start.Anchored = false
magic.Start.Position = wand.Point.Position
local weldConstraint = Instance.new('WeldConstraint')
weldConstraint.Parent = handle
weldConstraint.Part0 = handle
print(magic.Start)
weldConstraint.Part1 = magic.Start
end
if equipped then
if beamMode then
if not charging then
charging = true
print('Charging...')
for _, object in magic.Start:GetChildren() do
if object:IsA('ParticleEmitter') then
object.Transparency = NumberSequence.new(0)
-- Charging animation
end
end
else
charging = false
print('Released!')
-- Release Animation
for _, object in magic:GetChildren() do
for _, obj in object:GetChildren() do
if obj:IsA('Beam') or obj:IsA('ParticleEmitter') then
obj.Transparency = NumberSequence.new(0)
end
end
end
magic.End.Position = magic.Start.Position
local amount = 1000
local direction = (mousePosition.Position - magic.Start.Position).Unit
while addPosition(magic, direction, amount) do
task.wait(1)
end
end
else
print('not beammode')
end
end
end)
-- When mouse left click is released, it destroys cloned object called Magic
wand.Deactivated:Connect(function()
if not charging then
local magic = wand:FindFirstChild('Magic')
magic:Destroy()
end
end)
-- Part I keep changing and main part
function addPosition(magic, direction, amount)
local ray = Ray.new(magic.End.Position, direction * amount)
local part, position, normal = workspace:FindPartOnRay(ray, magic)
if part and part:IsDescendantOf(workspace) then
magic.End.Position = position
return true
end
return false
end
-- Localscript
local player = game:GetService('Players').LocalPlayer
local replicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = replicatedStorage.Wand.RemoteEvent
local num = 0
while true do
local mouse = player:GetMouse()
local hitPosition = mouse.Hit.Position
remoteEvent:FireServer(mouse, hitPosition)
num += 1
--print('Loop ' .. num)
task.wait(1)
end