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

How to create a part with the same position as mouse.hit.p?

Asked by 7 years ago
light=Instance.new("Part",game.Workspace)
light.Position=Vector3.new(mouse.Hit.p)

I have already found the mouse earlier in the script. I want to make it so the position of the light is equal to where I click my mouse Im also not getting any errors. halp pls.

2 answers

Log in to vote
3
Answered by 7 years ago
Edited 7 years ago

idk who downvoted you, I thought it was a reasonable question...

first off, you need to be using a local script in order to get Mouse.Hit.p

local script

light=Instance.new("Part",game.Workspace)
light.Position=mouse.Hit.p

Now, we have to access the local player and store it into a variable

local player = game.Players.LocalPlayer --store local player in player variable since this is a local script

light=Instance.new("Part",game.Workspace)
light.Position=mouse.Hit.p

Next, create a variable to store the mouse. With the local player we can use a special method by the name of LocalPlayer:GetMouse()

Check out more info about accessing the mouse here: http://wiki.roblox.com/index.php?title=API:Class/Mouse

local player = game.Players.LocalPlayer
local mouse = player:GetMouse() --We can now access the mouse with the local player

light=Instance.new("Part",game.Workspace)
light.Position=mouse.Hit.p

Finally, lets add a MouseButton1Down event to determine when to reset the brick position

local player = game.Players.LocalPlayer
local mouse = player:GetMouse() --We can now access the mouse with the local player

mouse.MouseButton1Down:connect(function() -- resets brick position every time mouse is pressed
    light=Instance.new("Part",game.Workspace)
    light.Position=mouse.Hit.p
end)

The above script should now work fine, but we forgot one important thing with this. Filtering Enabled! If we go to game.Workspace and look at the workspace properties, there should be a filtering enabled checkbox. When developing a game, we want to have that enabled so that hackers and exploiters can't mess up our game from their computer. This does leave us with a downfall, however. We cannot access or change things in the workspace such as creating a part in the above script, because filtering enabled will think we are a hacker. To prove that we are a local script running on the client, we have to use remote functions to create the new part and set it's position to where the mouse clicked

Filtering Enabled Tutorial: http://wiki.roblox.com/index.php?title=Client-Server_Model_and_FilteringEnabled <- this will have a more in depth coverage of how filtering enabled works, and some visuals to help you understand it.

http://wiki.roblox.com/index.php?title=RemoteFunction_and_RemoteEvent_Tutorial remote function and remote event tutorial here

For now lets add a remote function and insert parts of our script into a server script under workspace.

serverscript

light=Instance.new("Part",game.Workspace) --We can not do this in a local script anymore, so it has to be in a server script under game.Workspace
light.Position=mouse.Hit.p

local script

local player = game.Players.LocalPlayer
local mouse = player:GetMouse() --We can now access the mouse with the local player

Now we can create a remote function to link the two scripts...

serverscript

local part = Instance.new("Part", game.Workspace)
myRemoteFunction.Parent = game.Workspace --create a remote function
myRemoteFunction.Name = "MyRemoteFunction" --name it
function myRemoteFunction.OnServerInvoke(pos) --this will fire when the local script invokes it
    part.Position = pos --pos will be the mouse position passed as a parameter
end

local script

local player = game.Players.LocalPlayer
local mouse = player:GetMouse() --We can now access the mouse with the local player

mouse.MouseButton1Down:connect(function()
    game.Workspace.MyRemoteFunction:InvokeServer(mouse.Hit.p) -- We access MyRemoteFunction which we created in the server script and invoke it or make it run. Then we pass the mouse.Hit.p so that we can tell where to put the brick in the server script
end)

Hope that helps! Let me know if anything doesn't work and I will fix it asap!

Ad
Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
7 years ago

mouse.Hit.p is a Vector3 value. Therefore you're essentially doing this:

light.Position=Vector3.new(Vector3.new())

Obviously, this is flawed.

Just take out the unneeded Vector3 constructor:

light.Position = mouse.Hit.p
0
ty Nick1482 50 — 7y

Answer this question