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
3 years ago
Edited 3 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

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)
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 — 3y
0
You cannot construct a CFrame from a CFrame; you are able to pass Mouse.Hit through a Remote. Ziffixture 6913 — 3y

2 answers

Log in to vote
0
Answered by 3 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:

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)

0
Vector2 and UDim2 are both 2D, Vector3 is 3D. WideSteal321 773 — 3y
Ad
Log in to vote
0
Answered by 3 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:

local 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 — 3y
0
Gives me an error saying that hit is not a valid member of player MediaHQ 53 — 3y

Answer this question