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

How would you use the mouse in FilteringEnabled using remote E/F's? [EDITED AGAIN]

Kso

What im trying to do is get the position of mouse.hit in a ServerScript because my game is FilteringEnabled, however I have no idea how i'd do that without handling all of it in a localscript which doesent work. Ive seen people with Filtering games do stuff with the mouse before, so could someone give an example of how to do this?

EDIT: Used Perci1's code, and it works in Studio, but not in online + FE; it says that playerMouse (the variable i used in the serverscript to refer to the mouse) is nil whenever I shoot. Ill provide code if necessary.

EDIT2; Tried to set playerMouse to mouse.Hit.p; Threw a vector3 error.

The script

workspace.REgetmouse.OnServerEvent:connect(function(player, mouse)
	playerMouse = mouse.Hit.p
end)

debounce = false
Debris = game:GetService('Debris')

function workspace.RFshooting.OnServerInvoke(player, tuple)
	player.Character.Humanoid.shooting.Value = true
	while player.Character.Humanoid.shooting.Value == true and wait() do
		if not debounce and player and player.Character.Humanoid and player.Character.Humanoid.Health > 0 and player.Character.Torso and player.Character.Humanoid.bullets.Value > 0 then
			debounce = true
			local ammo = player.Character.Humanoid.bullets
			ammo.Value = ammo.Value - 1
			local spd = 225
			local cooldown = 0.08 -- 0.08
			local rng = 5
			local dmg = 12
				local bullet = Instance.new('Part', workspace)
			bullet.Name = 'bullet'
			bullet.CanCollide = false
			bullet.FormFactor = Enum.FormFactor.Custom
			bullet.Size = Vector3.new(0.3, 0.3, 5)
			bullet.Transparency = 0
			bullet.Position = player.Character.Torso.Position
			bullet.Material = Enum.Material.Neon
			bullet.BrickColor = BrickColor.Blue()
			bullet.Transparency = 0.2
				local light = Instance.new('SurfaceLight', bullet)
			light.Color = Color3.new(0.35, 0.65, 1)
				local direction = (playerMouse - player.Character.Torso.Position)
			bullet.CFrame = CFrame.new(player.Character.Torso.Position, player.Character.Torso.Position + direction)
			bullet.Velocity = bullet.CFrame.lookVector * spd
			Debris:AddItem(bullet, rng)
				local velocity = Instance.new('BodyForce', bullet)
			velocity.force = Vector3.new(0, bullet.Size.X * bullet.Size.Y * bullet.Size.Z * 196.2, 0)
				local particle = game.ReplicatedStorage.Laser.ParticleEmitter:Clone()
			particle.Parent = bullet
			local num = math.random(1, 2)
			if num == 1 then
				particle.Rotation = NumberRange.new(0, 0)
			else
				particle.Rotation = NumberRange.new(180, 180)
			end
			
			bullet.Touched:connect(function(hit)
				if hit and hit.Parent and hit.Parent:FindFirstChild('Humanoid') and hit.Parent ~= player.Character then
					hit.Parent.Humanoid:TakeDamage(dmg)
					bullet:Destroy()
				elseif hit.Parent == player.Character then
					
				elseif hit.Name ~= 'bullet' and hit.Name ~= 'aibullet' then
					bullet:Destroy()
				end
			end)
					
			wait(cooldown)
			debounce = false
		end
	end
end

2 answers

All you do is use a RemoteEvent or a RemoteFunction and pass the mouse as an argument. This lets you get the mouse in a local script, like normal, but then give it to a server script.

-ServerScript

event.OnServerEvent:connect(function(player, mouse) 
	print(mouse.Hit.p)
end)

-LocalScript

local mouse = game.Players.LocalPlayer:GetMouse()

event:FireServer(mouse)

3 comments

0

When I added this to my firing script and adjusted the mouse variables accordingly, it threw an error on mouse.Hit.p saying that hit is not a valid member of Player.

2

Add a player argument before the mouse argument in the server script, so it looks like this: function(player,mouse)

1

I knwo what's your error Perci1, Its in the ServerScript the parametre is player, mouse and not only mouse if you want to know what go on wiki I don't want to explain e.e

In light of the new code you've given, I believe I have a solution.

Instead of getting the LocalScript to communicate with the server, go backwards and let the server script communicate with a LocalScript.


Script changes:

Firstly, you want to get rid of the first three lines as they are not necessary for what I'm about to show you.

Then, you want to create a RemoteFunction somewhere and name it appropriately, let's assume you make a RemoteFunction in ReplicatedStorage called "GetMouse" (without the quotes). After that, add this line before the while loop on line 10, but after the setting the shooting value on line 9.

local playerMouse = game.ReplicatedStorage:WaitForChild("GetMouse"):InvokeClient(player) --Waits for the RemoteFunction then invokes it on the player that fired the RemoteEvent.

Since playerMouse will actually be the player's mouse, you will want to change your script so that where the playerMouse property is used, you'd change it to playerMouse.Hit.p


The new LocalScript:

Now we want to make a new LocalScript somewhere that is a descendant of the player so that it runs properly. Let's now think about what we're going to do:

The player has invoked the function, so we want to return the player's mouse for use by the script. How do we accomplish this?

We need to use a combination of the GetMouse method and the OnClientInvoke callback to accomplish what we want to do.

local rs = game.ReplicatedStorage --Variable for ReplicatedStorage
local getMouse = rs:WaitForChild("GetMouse") --Get the RemoteFunction.

getMouse.OnClientInvoke = function() --Create the function that occurs when the RemoteFunction is invoked.
	return game.Players.LocalPlayer:GetMouse() --Gets the player's mouse then returns it for use in the playerMouse variable.
end

If you test your script now, it should work.


I hope my answer helped you. If it did, be sure to accept it.

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