Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you find the mouse's vector3 value?

Hi, I'm trying to set a parts position to the mouse's position. I've tried using mouse.Target, mouse.Hit and mouse.X / mouse.Y.

Current code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local SpellPart = game.Workspace.Spell


local function SpellSummon()
	local MT = mouse.Target
	
	SpellPart.Position = MT
	
end

mouse.Button1Down:Connect(function()
	SpellSummon()
end)

Thanks for reading!

2 comments

0

Are you trying to get the mouse's current position

0

Because if you are you can use this function to get its position - local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.Move:Connect(function(move) print(mouse.X) print(mouse.Y) end)

1 answer

✓ Accepted answer

hey, you're already on the right track. the reason this isn't working is because you are trying to create a part from the client, or from the player's computer. this is a whole topic all in itself and I suggest you read this article and this article, as it prevents exploiters.

what you will need to do this behavior is an object called a remote event, which is essentially a signal that allows both types of scripts to communicate. you can put a remote event in ReplicatedStorage where both the player's computer and the server can see it. you will need two different scripts for the client and the server (aka a localscript and a serverscript)

for client

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	local spellEvent = ReplicatedStorage.Remotes.Spell -- this is your remote event, I like to use a folder called Remotes
	spellEvent:FireServer(mouse.Hit.Position)
end)

for server

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local spellEvent = ReplicatedStorage.Remotes.Spell

spellEvent.OnServerEvent:Connect(function(_, position)
	local spell = Instance.new("Explosion") -- you can replace this explosion with your part
	spell.Position = position
	spell.Parent = game.Workspace
end)

1 comment

0

Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.