Whats wrong with this

Explanation

Basically I want the script to shoot out a sphere and for it to face the direction I want it to based off of the RayCast but I am having trouble, when I click with the tool it goes to random places and I can’t figure out why.

Video

local Script in tool

--// SERVICES
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer


--// MISC
local Tool = script.Parent
local mouse = player:GetMouse()
local Animation = Tool.ToolAnimation
local Orb1FireEvent = ReplicatedStorage.AtlantisFolder.Orb1Folder.Orb1Fire
local Debounce = false
local CoolDown = 2


--// FUNCTIONS
mouse.TargetFilter = game.Workspace.BulletFolder

Tool.Activated:Connect(function()
	if Debounce then return end
	Debounce = true
	local mousePosition = mouse.Hit.p
	local originPosition = Tool.Handle.OrbPlaceHolder.Position
	
	Orb1FireEvent:FireServer(mousePosition, originPosition)
	wait(CoolDown)
	Debounce = false
end)

Regular Script in ServerScriptService

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")


local TravDist = 50
local OrbDamage = 5


local function CreateOrb(origin, direction)
	local midPoint = origin+direction/2
	
	local NewOrb = ReplicatedStorage.AtlantisFolder.Orb1Folder.Orb1:Clone()
	NewOrb.Parent = workspace.BulletFolder
	NewOrb.CFrame = CFrame.new(midPoint, origin)
	
	Debris:AddItem(NewOrb, 1)
end

local OrbFireEvent = ReplicatedStorage.AtlantisFolder.Orb1Folder.Orb1Fire

OrbFireEvent.OnServerEvent:Connect(function(player, mousePos, originPos)
	local direction = (mousePos - originPos).Unit * TravDist
	
	local result = workspace:Raycast(originPos, direction)
	
	if result then
		local character = result.Instance.Parent
		local humanoid = character:FindFirstChild("Humanoid")
		
		if humanoid and humanoid ~= player.Character.Humanoid then
			humanoid:TakeDamage(OrbDamage)
		end
	end
	CreateOrb(originPos, direction)
end)

Wait a minute what exactly are you trying to do. Do you want the sphere to appear where your mouse clicks?

1 Like

Well I was doing that just to test my progress but the end goal is to have it shoot to where my mouse is pointing

So basically you want the sphere to appear where you click.

1 Like

Well I want it to shoot from where the invisible sphere is as you can see in the video, to whereever my mouse click, and have the front face of the sphere always poiting towards where my mouse clicked, almost like using velocity if that makes sense

Alright I’m going to quickly recreate this on my end and try to figure out what the issue is.

1 Like

Thank you very much I appreciate the help if you want I can send you the model for it

I mean idk from what you described it seems the script works for me at least. The part gets teleported in the direction of the mouse click and only goes 50 studs.

1 Like

In the video you can see me click directly on a NPC, and it doesnt go to the correct position

I mean idk is this what you wanted test game for random guy - Roblox

Ok more or less this is what I could come up with. Lemme know if it works.

local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")


local TravDist = 50
local OrbDamage = 5

local LIFETIME = 1
local SPEED = 15
local GRAV = 0

function DestroyBullet(bullet)
	bullet:Destroy()
end

function shoot(startpos, direction, player, tempbullet)
	local bullet =  tempbullet:Clone()
	
	bullet.Position = startpos
	bullet.Parent = workspace

	local RaycatsParams = RaycastParams.new()
	RaycatsParams.FilterType = Enum.RaycastFilterType.Blacklist
	RaycatsParams.FilterDescendantsInstances = {player.Character}

	local lastPos = nil
	local startTime= time()
	local bulletStepper
	bulletStepper = RunService.Heartbeat:Connect(function(dt)
		local t = time() - startTime
		if t >= LIFETIME then

			bulletStepper:Disconnect()

			DestroyBullet(bullet)
			return

		end

		local moveDir = direction * SPEED * dt - Vector3.new(0,GRAV*t,0)
		local rayResult = workspace:Raycast(bullet.Position, moveDir, RaycatsParams)
		if rayResult ~= nil then
			bulletStepper:Disconnect()
			if player == game.Players.LocalPlayer then
				game.ReplicatedStorage.Hit:FireServer(rayResult.Instance)
			end

			DestroyBullet(bullet)
		else
			if lastPos then
				direction = (bullet.Position - lastPos).Unit
			
			end

			lastPos = bullet.Position
			bullet.Position += moveDir
		end
	end)
end

local function CreateOrb(origin, direction)
	local midPoint = origin+direction/2

	local NewOrb = ReplicatedStorage.AtlantisFolder.Orb1Folder.Orb1:Clone()
	NewOrb.Parent = workspace.BulletFolder
	NewOrb.CFrame = CFrame.new(midPoint, origin)

	Debris:AddItem(NewOrb, 1)
end

local OrbFireEvent = ReplicatedStorage.AtlantisFolder.Orb1Folder.Orb1Fire

OrbFireEvent.OnServerEvent:Connect(function(player, mousePos, originPos)
	local direction = (mousePos - originPos).Unit * TravDist

	local result = workspace:Raycast(originPos, direction)

	if result then
		local character = result.Instance.Parent
		local humanoid = character:FindFirstChild("Humanoid")

		if humanoid and humanoid ~= player.Character.Humanoid then
			humanoid:TakeDamage(OrbDamage)
		end
	end
	shoot(originPos, direction, player, ReplicatedStorage.AtlantisFolder.Orb1Folder.Orb1)
	--CreateOrb(originPos, direction)
end)
1 Like

I appreciate you being being so dedicated to tryna help me solve this you’re awesome!