Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you find the mouse's vector3 value?

Asked by 3 years ago
Edited 3 years ago

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!

0
Are you trying to get the mouse's current position bestshot123 38 — 3y
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) bestshot123 38 — 3y

1 answer

Log in to vote
1
Answered by
Speedmask 661 Moderation Voter
3 years ago
Edited 3 years ago

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)
0
Thanks! TheStarRblx 27 — 3y
Ad

Answer this question