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

[FIXED] How to spawn a model at a players mouse with a tool?

Asked by
MediaHQ 53
4 years ago
Edited 4 years ago

I want to make a tool that spawns a model at the player's mouse, here is my code if you want it

Localscript

1local player = game.Players.LocalPlayer
2local RS = game:GetService("ReplicatedStorage")
3local Mesa = RS.Towers.Mesa
4local Mouse = player:GetMouse()
5 
6script.Parent.Activated:Connect(function()
7    script.Parent.SpawnTower:FireServer(Mouse)
8end)

ServerScript

1local RS = game:GetService("ReplicatedStorage")
2local Mesa = RS.Towers.Mesa
3 
4script.Parent.SpawnTower.OnServerEvent:Connect(function(Mouse)
5    local newtower = Mesa:Clone()
6    newtower.Parent = game.Workspace
7    newtower:MoveTo(Mouse.Position)
8    newtower.Name = "Mesa Tower"
9end)

Instead of spawning at the player's mouse, it just spawns the model where it originally was. How fix?

Fix: localscript

1local player = game.Players.LocalPlayer
2local RS = game:GetService("ReplicatedStorage")
3local Mesa = RS.Towers.Mesa
4local Mouse = player:GetMouse()
5 
6script.Parent.Activated:Connect(function()
7    local Mousepos = Mouse.Hit.p --I need to get the mouse position right before firing the event
8    script.Parent.SpawnTower:FireServer(Mousepos) --Send the mouse pos to the server
9end)

ServerScript

01local RS = game:GetService("ReplicatedStorage")
02local Mesa = RS.Towers.Mesa
03local tool = script.Parent
04local player = tool.Parent.Parent
05 
06script.Parent.SpawnTower.OnServerEvent:Connect(function(player, Mouse) -- Player is the first arguement always when firing from local
07    local newtower = Mesa:Clone()
08    newtower.Parent = game.Workspace
09    newtower:MoveTo(Mouse) --We already have the mouse pos so move to mouse
10    newtower.Name = "Mesa Tower"
11end)
0
Mouse.Hit represents the CFrame, in 3D space, of where the mouse clicked on the player's screen. You can't pass Hit itself to the server, but you can pass Hit wrapped in a CFrame.new() constructor to the server. It sounds weird and is weird, but it works. DeceptiveCaster 3761 — 4y
0
You cannot construct a CFrame from a CFrame; you are able to pass Mouse.Hit through a Remote. Ziffixture 6913 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

Mouse.Position returns a UDim2 value (I assume, it might be Vector2) so you'll need a lot more code than that, here is an example of a UDim2 value:

1local pos = UDim2.new(xoffset,0,yoffset,0)

Here is an example of a Vector3 value:

1local pos = Vector3.new(x, y, z)

Here is an example of a Vector2 value:

1local pos = Vector2.new(x,y)

Accept this answer if it helped! (No one on here is giving you code, you're here to learn, not be spoon-fed the answer)

0
Vector2 and UDim2 are both 2D, Vector3 is 3D. WideSteal321 773 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Mouse.Position does not return a Vector3 value, so you are going to need to look at this from another angle.

When trying to find the position where the mouse clicked in the games 3D space, we use one of the mouse's properties called Hit.

Here is an example of the Hit property:

1local position = mouse.Hit.p

I hoped this helps, commend below if you need further assistance.

0
Also note, that mouse.Hit returns a CFrame value, so we use the '.p' part to get a Vector3 value from it. papasherms 168 — 4y
0
Gives me an error saying that hit is not a valid member of player MediaHQ 53 — 4y

Answer this question