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

How to get a players mouse using a non-local script inside of a tool?

I have a script that is supposed to fire a projectile out of a parent tool, however the script ends when I try to define the players mouse

local plr = script.Parent.Parent:findFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(plr)
local mouse = player:GetMouse()

This is what I have, however it doesn't work.

Alternatively I have tried

local plr = script.Parent.Parent
local name = plr.Name
local player = game.Players.name
local mouse = player:GetMouse()

The tool parent ladder is

script > tool > players backpack/player(when held)

Any ideas on how to fix this?

3 comments

0

Is there any reason why you're using a serverscript instead of a localscript? Because a localscript would be a much better type of script to use for your situation

0

You can't

0

I am using a serverscript because the tool spawns a solid block that I want to be able to interact with all players, not just the client player

3 answers

The ONLY way this would work, is by setting up a RemoteEvent that would constantly firing, firing the mouse from a LOCAL script. It's recommended you get the player's mouse locally, since it's the most efficient way.

1 comment

0

I really don't know how i'd be able to get the mouse from a local script then send it to the regular script

For a RemoteEvent / FilteringEnabled method (Which is highly advised you learn how to use because it's a good system), you're going to need; A localscript, A normal server script, A remote event.

local player = game:GetService'Players'.LocalPlayer
local mouse = player:GetMouse()
local event = script.Parent.RemoteEvent

mouse.Button1Down:connect(function()
	event:FireServer()
end)

That code will go into the localscript.

local event = script.Parent.RemoteEvent

event.OnServerEvent:connect(function()
-- Your projectile code will go here.
end)

This code will go into the Server script.

I'm not entirely sure if that is the best method, and you could most likely improve it.

1 comment

0

While this is useful information, I need to tell where the players mouse is pointing as well as when it clicks

i can only say that you must put this script in StarterPlayer.StarterPlayerScripts:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse() --it works for me in StarterPlayerScripts
workspace.yes.Activated:Connect(function()
	mouse.Button1Down:connect(function()
	
end)
end)

i know you wanted to know how to put it into a tool but it works like this for me

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