I am doing a simple building script. I want to get the mouse of the localplayer to clone a part into a carpet on workspace. The script that i tested works (sort of), but what happens is that other players cannot see the blocks you placed. is there any way to get the mouse of a player without a localscript? Thank you!
No you can't do :GetMouse()
on a normal script. This is because the server does not have a mouse - the server is not a player it is a machine. What you should be doing instead is using remote events and remote functions whenever you want something from the client to be sent over to the server, like pressing a button or getting the mouse's position.
1 | -- in a local script |
2 | local Player = game:GetService( "Players" ).LocalPlayer |
3 |
4 | while wait( 1 ) do |
5 | local Mouse = Player:GetMouse() |
6 | RemoteEvent:FireServer(Mouse.Hit.p) |
7 | end ) |
And then:
1 | -- in a server script |
2 |
3 | RemoteEvent.OnServerEvent:Connect( function (player, mousePosition) |
4 | print (player.Name.. " has his mouse at " ..mousePosition) |
5 | end ) |
I think you are competent enough to know how to fit this into your script without further help.
Edit (you didn't know how to):
01 | --this is located on ServerScriptService |
02 | local Plain = game.Lighting.Blocks.Plain |
03 | local build = Instance.new( "RemoteEvent" ) |
04 | build.Parent = game.ReplicatedStorage |
05 | build.Name = "BuildEvent" |
06 |
07 | local function InsertPart (player, mousePosition) |
08 | local Clone = Plain:Clone() |
09 | Clone.Parent = game.Workspace.BuildParts |
10 | Clone.Position = mousePosition |
11 | script.Parent.Handle [ "Pop!" ] :Play() |
12 | end |
13 | build.OnServerEvent:Connect(InsertPart) |
14 |
15 | -- in a local script in the tool |
:GetMouse() is only on the client, you would have to use RemoteEvents to transfer the data from the client to server here you can look what remote events are
I have a new problem: I've followed the steps of this tutorial: https://www.youtube.com/watch?v=3wkPekjQvKg and the same problem happens when you put GetMouse() into a regular script i'll give you the piece of code
01 | --this is located on ServerScriptService |
02 | local build = Instance.new( "RemoteEvent" ) |
03 | build.Parent = game.ReplicatedStorage |
04 | build.Name = "BuildEvent" |
05 | local function InsertPart () |
06 | local Tool = script.Parent |
07 | local Players = game:GetService( "Players" ) |
08 | local player = game.Players.LocalPlayer |
09 | local mouse = player:GetMouse() |
10 |
11 | --Building blocks-- |
12 | local Plain = game.Lighting.Blocks.Plain |
13 | --Function-- |
14 | function onActivated() |
15 | local clone = Plain:Clone() |
and here's the tool
1 | local build = game.ReplicatedStorage:WaitForChild( "BuildEvent" ) |
2 | build:FireServer() |