Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
Smunkey 95
8 years ago

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?

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 TurboFusion 1821 — 8y
0
You can't koolkid8099 705 — 8y
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 Smunkey 95 — 8y

3 answers

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
8 years ago

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.

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 Smunkey 95 — 8y
Ad
Log in to vote
0
Answered by
ITBV 0
8 years ago

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.

0
While this is useful information, I need to tell where the players mouse is pointing as well as when it clicks Smunkey 95 — 8y
Log in to vote
0
Answered by 4 years ago

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

Answer this question