I want to make a tool that spawns a model at the player's mouse, here is my code if you want it
Localscript
local player = game.Players.LocalPlayer local RS = game:GetService("ReplicatedStorage") local Mesa = RS.Towers.Mesa local Mouse = player:GetMouse() script.Parent.Activated:Connect(function() script.Parent.SpawnTower:FireServer(Mouse) end)
ServerScript
local RS = game:GetService("ReplicatedStorage") local Mesa = RS.Towers.Mesa script.Parent.SpawnTower.OnServerEvent:Connect(function(Mouse) local newtower = Mesa:Clone() newtower.Parent = game.Workspace newtower:MoveTo(Mouse.Position) newtower.Name = "Mesa Tower" end)
Instead of spawning at the player's mouse, it just spawns the model where it originally was. How fix?
Fix: localscript
local player = game.Players.LocalPlayer local RS = game:GetService("ReplicatedStorage") local Mesa = RS.Towers.Mesa local Mouse = player:GetMouse() script.Parent.Activated:Connect(function() local Mousepos = Mouse.Hit.p --I need to get the mouse position right before firing the event script.Parent.SpawnTower:FireServer(Mousepos) --Send the mouse pos to the server end)
ServerScript
local RS = game:GetService("ReplicatedStorage") local Mesa = RS.Towers.Mesa local tool = script.Parent local player = tool.Parent.Parent script.Parent.SpawnTower.OnServerEvent:Connect(function(player, Mouse) -- Player is the first arguement always when firing from local local newtower = Mesa:Clone() newtower.Parent = game.Workspace newtower:MoveTo(Mouse) --We already have the mouse pos so move to mouse newtower.Name = "Mesa Tower" end)
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:
local pos = UDim2.new(xoffset,0,yoffset,0)
Here is an example of a Vector3
value:
local pos = Vector3.new(x, y, z)
Here is an example of a Vector2
value:
local 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)
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:
local position = mouse.Hit.p
I hoped this helps, commend below if you need further assistance.