Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can i make GetMouse() without a localscript?

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!

1 comment

0

thanks guys very cool

3 answers

✓ Accepted answer

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.

-- in a local script
local Player = game:GetService("Players").LocalPlayer

while wait(1) do
	local Mouse = Player:GetMouse()
	RemoteEvent:FireServer(Mouse.Hit.p)
end)

And then:

-- in a server script

RemoteEvent.OnServerEvent:Connect(function(player, mousePosition)
	print(player.Name.." has his mouse at "..mousePosition)
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):

--this is located on ServerScriptService
local Plain = game.Lighting.Blocks.Plain
local build = Instance.new("RemoteEvent")
build.Parent = game.ReplicatedStorage
build.Name = "BuildEvent"

local function InsertPart (player, mousePosition)
	local Clone = Plain:Clone()
	Clone.Parent = game.Workspace.BuildParts
        Clone.Position = mousePosition
        script.Parent.Handle["Pop!"]:Play()
end
build.OnServerEvent:Connect(InsertPart)

-- in a local script in the tool
local player = game.Players.LocalPlayer
local build = game.ReplicatedStorage:WaitForChild("BuildEvent")

function onActivated()
	local mousePosition = player:GetMouse().Hit.p    
	build:FireServer(mousePosition)
end
    
script.Parent.Activated:connect(onActivated)

5 comments

0

i have just heard about remoteevents, i have no idea what to do

0

Read through the Wiki, that's what.

0

i've read it when danklua sent me the answer, still don't understand

0

i should also mention that i'm doing a tool

0

Create a remote event (it's an insertable object), and instead of creating a block on the client, fire the remote event with the mouse's position; connect a server script to that remote event and create the block there.

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

--this is located on ServerScriptService
local build = Instance.new("RemoteEvent")
build.Parent = game.ReplicatedStorage
build.Name = "BuildEvent"
local function InsertPart ()
	local Tool = script.Parent
	local Players = game:GetService("Players")
	local player = game.Players.LocalPlayer
	local mouse = player:GetMouse()

	--Building blocks--
	local Plain = game.Lighting.Blocks.Plain
	--Function--
	function onActivated()
		local clone = Plain:Clone()
		clone.Parent = game.Workspace.BuildParts
		Plain.Position = mouse.Hit.Position
		script.Parent.Handle["Pop!"]:Play()
	end
	script.Parent.Activated:connect(onActivated)

end
build.OnServerEvent:Connect(InsertPart)

and here's the tool

local build = game.ReplicatedStorage:WaitForChild("BuildEvent")
build:FireServer()

3 comments

0

I've edited my answer have a look and tell me if it works.

0

Did my answer work? Accept it if it did and if it didn't tell me what went wrong.

0

Yes, it did.

This discussion is preserved as it was. New answers, comments, and votes are closed.