Parts follow mouse

Hi so i made a localscript which fires a remoteEvent when tool is activated and will pass the mouse.hit.position
when the remoteEvent is fired, it’ll create 50 parts that will go to the player’s mousePosition
heres the result:

localscript:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent.Parent
local remoteEvent = game.ReplicatedStorage.powerEvents.initialPower2.power

local debounce = false
tool.Activated:Connect(function()
    if debounce == false then
        debounce = true
        local mousePos = mouse.Hit.Position
        mouse.TargetFilter = workspace:WaitForChild(player.Name.."'s K-BOT")
        remoteEvent:FireServer(mousePos)
    end
end)

remoteEvent.OnClientEvent:Connect(function()
    wait(5)
    debounce = false
end)

serverscript:

local remoteEvent = initialEvent.power

remoteEvent.OnServerEvent:Connect(function(player, mousePos)
    local debris = game:GetService("Debris")
    local kbot = workspace:WaitForChild(player.Name.."'s K-BOT")
    local val = 1

    while wait() do
        local part = Instance.new("Part", workspace)
        part.Size = Vector3.new(2,2,2)
        part.Shape = 'Ball'
        part.Material = 'Neon'
        part.BrickColor = BrickColor.new('Really red')
        part.Anchored = true
        part.CanCollide = false
        part.CFrame = kbot.RootPart.CFrame
        debris:AddItem(part, 5)
        local tweenService = game:GetService("TweenService")
        local tween = tweenService:Create(part, TweenInfo.new((kbot.RootPart.Position - mousePos).Magnitude/100, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Position = mousePos})
        local loadAnimation = kbot.Humanoid.Animator:LoadAnimation(kbot.animations.Power1)
        loadAnimation:Play()
        tween:Play()
        val = val + 1
        if val == 50 then
            break
        end
    end
    remoteEvent:FireClient(player)
end)

However it’s not really the result i wanted.
I want the created parts to move along the mouse when its moving, how would i do that?
something like this btw.

2 Likes

Ill try to figure out what is wrong, but see if using this function instead of mouse.Hit.Position works.

local userInputService = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
local range = ? --insert number here
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {workspace:WaitForChild(player.Name.."'s K-BOT")}
RayParams.FilterType = Enum.RaycastFilterType.Blacklist

local function getWorldMousePos()
	local mousePos = userInputService:GetMouseLocation()
	local ray = camera:ViewportPointToRay(mousePos.X,mousePos.Y)
	local direction = ray.Direction * range
	local result = workspace:Raycast(ray.Origin, direction, RayParams)
	if result then
		return result.Position
	else
		return nil --return nothing because it isn't touching ground
	end
end
1 Like

I dont know if the purpose of that was another way to get the mouse location?
But it didn’t work it still go to position where the mouse was clicked

yes, it is just a better way of getting the mouse’s position. But I will try to find your problem.

shootTest.rbxl (30.7 KB)
I made this if you want to try to implement it into what you are trying to do, I hope it helps :slight_smile: