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

How do I get a certain player mouse?

So I'm making a turret, a switch has to be stepped on in order to activate the turret, the turret will always look at the player's mouse. But how do I get the mouse of the player that stepped on the button instead of getting everyone's mouse? Here is the script that makes the turret follow the mouse (Everyone's mouse?)

This Script is not a LocalScript, and it is inside Workspace.

function FollowMouse(p)
	script.Parent.ControllableTurret1.ActivatePart.Touched:connect(function()
		repeat wait() until game.Workspace.ControllableTurret1.TestingCannon.SpinningPart.Base
		while true do
			local pos
			local player = game.Players.LocalPlayer
			local m = player:GetMouse()
			local t = m.Hit
			if t then
				pos = t
				local base = game.Workspace.ControllableTurret1.TestingCannon.SpinningPart:WaitForChild("Base") 
				local h = player.Character:WaitForChild("Head")
				base.CFrame = CFrame.new(base.Position,Vector3.new(t.X,t.Y,t.Z))
				--tor["Neck"].C0 = CFrame.new(0,1,0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y) + 1.55,3.15,0)
			end
			wait()
		end
	end)
end

FollowMouse()

^Also this Script is disabled^, an another script would activate this script. If you want to see that script as well, here it is:

local effectPart = game.Workspace.ControllableTurret1.EffectPart
local activePart = game.Workspace.ControllableTurret1.ActivatePart
local sp = script.Parent
local followMouse = game.Workspace.FollowMouseScript
local fireballControl = sp.FireballControl

local buttonPressed = false
--Store whether the button is pressed in a local variable
activePart.Touched:connect(function(hit)
    if not buttonPressed then
    -- Is it not pressed?
        buttonPressed = true
        -- Mark it as pressed, so that other handlers don't execute
		if hit.Parent and activePart.BrickColor ~= BrickColor.new("Sea green") then
			local hum = hit.Parent:FindFirstChild("Humanoid")
			if hum and effectPart.BillboardGui.StatusText.Text ~= "COOLDOWN" or hum and effectPart.BillboardGui.StatusText.Text ~= "ACTIVATED" then
				activePart.BrickColor = BrickColor.new("Sea green")
				effectPart.BrickColor = BrickColor.new("Sea green")
				effectPart.BillboardGui.StatusText.Text = "ACTIVATED"
				effectPart.BillboardGui.StatusText.TextColor3 = Color3.new(0,255,0)
				activePart.Active:Play()
				game.Workspace.ControllableTurret1.TestingCannon.SpinningPart.Base.Material = "Neon"
				game.Workspace.ControllableTurret1.TestingCannon.SpinningPart.Barrel.Material = "Neon"
				activePart.ParticleEmitter.Enabled = true
				sp.CannonStuff.Ammo.Visible = true
				sp.FireballControl.Disabled = false
				followMouse.Disabled = false
			end
		end
        wait(10) -- Amount of time before it deactivates again.
 		activePart.BrickColor = BrickColor.new("Cool yellow")
		effectPart.BrickColor = BrickColor.new("Cool yellow")
		effectPart.BillboardGui.StatusText.Text = "COOLDOWN"
		effectPart.BillboardGui.StatusText.TextColor3 = Color3.new(255,255,0)
		activePart.Deactive:Play()
		game.Workspace.ControllableTurret1.TestingCannon.SpinningPart.Base.Material = "SmoothPlastic"
		game.Workspace.ControllableTurret1.TestingCannon.SpinningPart.Barrel.Material = "SmoothPlastic"
		sp.CannonStuff.Ammo.Visible = false
		activePart.ParticleEmitter.Enabled = false
		activePart.ParticleEmitter2.Enabled = true
		sp.FireballControl.Disabled = true
		followMouse.Disabled = true
		wait(7) -- Cooldown
     	activePart.BrickColor = BrickColor.new("Dark stone grey")
		effectPart.BrickColor = BrickColor.new("Dark stone grey")
		if sp.CannonStuff.Ammo.Amount.Value > 0 then
			effectPart.BillboardGui.StatusText.Text = "AVAILABLE"
		elseif sp.CannonStuff.Ammo.Amount.Value == 0 then
			effectPart.BillboardGui.StatusText.Text = "NO AMMO, AVAILABLE."
			effectPart.BillboardGui.StatusText.TextColor3 = Color3.new(255,0,0)
		end
		activePart.ParticleEmitter2.Enabled = false
		effectPart.BillboardGui.StatusText.TextColor3 = Color3.new(255,255,255)
		buttonPressed = false
        -- Mark it as not pressed, so other handlers can execute again
    end
end)

^This script is inside a GUI. (That is inside StarterGUI)^ And it's a Script, not a LocalScript.

2 comments

0

Is FE on? If so, Scripts won’t work in GUI.

0

No, FE is off.

3 answers

✓ Accepted answer

Use your idea but with a local script. put this in a local script within startergui and have the local script disabled

while true do
            local player = game.Players.LocalPlayer
            local pos = player:GetMouse().Hit
            if pos then
                local base = game.Workspace.ControllableTurret1.TestingCannon.SpinningPart:WaitForChild("Base") 
                local h = player.Character:WaitForChild("Head")
                base.CFrame = CFrame.new(base.Position,Vector3.new(t.X,t.Y,t.Z))
                --tor["Neck"].C0 = CFrame.new(0,1,0) * CFrame.Angles(-math.asin((m.Origin.p - m.Hit.p).unit.y) + 1.55,3.15,0)
            end
            wait()
        end

then just add this to the touched function in the activation script

if hit.Parent then
	if game.Players:FindFirstChild(hit.Parent.Name) then
		followMouse = game.Players:FindFirstChild(hit.Parent.Name).PlayerGui.FollowMouseScript
	end
end

2 comments

0

Thank you :)

0

No problem. What is the game, I want to see this turret in action.

You cant use LocalPlayer in a server script.

3 comments

0

So what do I do about that? :V

0

use a local script

0

Ok, I placed the code into a LocalScript and I put it in StarterGui cuz I think it has something to do with that. But then an error came up, this error however has nothing to do with this..

You cannot use Mouse or LocalPlayer on the server, you have to use a local script in a player instead.

1 comment

0

So I have to find a way to put a LocalScript inside the player that stepped on the switch?

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