I have created a system that makes this part follow the mouse, but I’m struggling with the y-axis. So far the only solution I have come up with is to base the position off of the mouse’s target, but it’s really glitchy. Here is my code so far:
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Move:Connect(function()
local name = game.Players.LocalPlayer.Name
local part = game.Workspace:WaitForChild(name.."lightning")
local basePos = mouse.Target.Position.Y
local baseSize = mouse.Target.Size.Y / 2
local newPos = basePos + baseSize
part.Position = Vector3.new(mouse.Hit.Position.X, newSize, mouse.Hit.Position.Z)
end)
Look into raycasting, this will allow you to blacklist/filter out the sphere so the mouse.Hit.p doesn’t react to the sphere
1 Like
the target is probably becoming the ball, you might wanna add the ball to the TargetFilter list
1 Like
Blacklisting the sphere just made it output an error, ignoring the rest of my code. I tried setting CanQuery to false as well, which did the same thing.
Is the script you sent in the first post what you actually used or did you just write parts of the code you actually have? Because you’re using newSize for the Y position and not newPos so as far as I’m aware the original script should error because newSize doesn’t exist… So if you change newSize to newPos then the original script looks like it just needs a raycast and it’s good to go?
Sorry, that was a typo. Assume that both newSize and newPos are just newPos
Also, what do you mean by raycast? Doesn’t mouse.Target already raycast from the mouse? Sorry if this sounds dumb, I’m relatively new to scripting.
Okay so it turns out blacklisting it did work and I was just using a test script instead of the actual one. This is the final code:
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Move:Connect(function()
local name = game.Players.LocalPlayer.Name
local part = game.Workspace:WaitForChild(name.."lightning")
mouse.TargetFilter = part
local basePos = mouse.Target.Position.Y
local baseSize = mouse.Target.Size.Y / 2
local newPos = basePos + baseSize
part.Position = Vector3.new(mouse.Hit.Position.X, newPos, mouse.Hit.Position.Z)
end)
1 Like
Could be that it has something built in, I’m not quite up to date on the canquerry and mouse.hit has had changes. A common problem people face with mouse.Hit is that the ghost/part that’s placed on the mouse’s position will block the line from the camera to the surface where you want to aim because mouse.Hit doesnt care if a part is hollow, invisible, no collisions etc.
Therefor raycast can be used to basically tell mouse.Hit to completely ignore that part and only focus on other parts, raycast is super common for ignoring the character for building systems so you can build blocks on your own character etc
Edit, seems the mouse.TargetFilter basically makes it much easier to filter than writing a full raycast which can be really annoying and very confusing for newer scripters
1 Like
Thanks for the info, I will definitely look more into raycasting!
1 Like