Mouse target script is not working and has no output errors?

So I have this scirpt that should do all this once clicked, but does no work, any help?

local Player = game.Players.LocalPlayer

local Character = Player.Character

local mouse = Player:GetMouse()

local Hum = Character:WaitForChild("Humanoid")

local Tool = game.ReplicatedStorage.Controller:Clone()

local chicken = game.Workspace.ControllerB.Chinny:Clone()

if mouse.Target then

if mouse and mouse.Target.Name == 'ControllerB' and (Player.Character.UpperTorso.Position - mouse.Hit.p).magnitude <= 10 then

if mouse.Target.Occupied == false then

Tool.Parent = Character

Hum:EquipTool(Tool)

game.Workspace.Couch.CanCollide = false

chicken.Parent = Player.PlayerGui

game.ReplicatedStorage.PlayGame2:FireServer()

end

end

end

after testing some prints, it seems to end at “if mouse.Target then”

It’s because you’re just running the conditional statement at the start of the script. It’ll run once and then never run again. You’ll want to create a loop in order so that every so often it checks for the mouse’s target. You can use while loops, for loops, etc, but if you want something that runs as fast as possible, you’ll want to use either RenderStepped or Heartbeat.

Which uses RunService, so

game:GetService("RunService").RenderStepped:connect(function()
 -- code you want to constantly run
end)

You won’t put the whole of your code in there, just the code you want to be repeated, variables can be initialised outside of the loop.

Im confused on what I would do with this, you say only the code I want repeated, but I do not understand, any help?

Well, technically you can put all the code inside there, it just wouldn’t be very efficient. It would be a good idea to read up on loops to understand how they work, if you don’t already.

Everything within the RenderStepped function will continuously run every frame (see the wiki pages linked in the previous post), therefore the code that you want inside will keep running.

E.g.

game:GetService("RunService").RenderStepped:connect(function()
local val= 5
print(val)
end)

This would repeatedly create a variable called val, set the value of val to 5 and then print the value of val (5).

In contrast:

E.g.

local val= 5
game:GetService("RunService").RenderStepped:connect(function()
print(val)
end)

This will create a variable called val with the value of 5 then repeatedly print out its value, which is more efficient in the long run, and scales depending on your codes size.

So doing something like would be fine

local Player = game.Players.LocalPlayer

local Character = Player.Character

local mouse = Player:GetMouse()

local Hum = Character:WaitForChild("Humanoid")

local Tool = game.ReplicatedStorage.Controller:Clone()

local chicken = game.Workspace.ControllerB.Chinny:Clone()

game:GetService("RunService").RenderStepped:connect(function()
if mouse.Target then

if mouse and mouse.Target.Name == 'ControllerB' and (Player.Character.UpperTorso.Position - mouse.Hit.p).magnitude <= 10 then

if mouse.Target.Occupied == false then

Tool.Parent = Character

Hum:EquipTool(Tool)

game.Workspace.Couch.CanCollide = false

chicken.Parent = Player.PlayerGui

game.ReplicatedStorage.PlayGame2:FireServer()

end

end

end
end) 

Scripts run once. That code will check for the target once, and never again. To fix this you’ll need to run it in a loop like the one posted above. This way the code will keep repeating continuously and you’ll achieve the desired behaviour.

This didn’t work, and again still no output errors.

I will try to edit the code a little more to see if I can make something work

It was just an example of what you could do. You’ll want to make sure your script is a LocalScript, not a server-side script. Some of your code could be changed as well, such as “if mouse.Target then if mouse”, if mouse.Target was there, then mouse would also be there.

It is a local script, ill be trying my best to fix this.

Try run print statements before and after your conditional statements (the if {condition} then), that’ll help tell you whether its a logic error that you’re falling into or something else.

I did what you said, removed the .target, it gave me error, so I removed the other “if mouse” and im getting a output full of "attempt to index field ‘Target’

I think you misinterpreted what I wanted to say. I assume you’re doing something like “if Target then”, if you are, that’s wrong. I was referring to your 3 conditional statements, where your first statement was pointless. Basically:

Change this
game:GetService("RunService").RenderStepped:connect(function()
if mouse.Target then

if mouse and mouse.Target.Name == 'ControllerB' and (Player.Character.UpperTorso.Position - mouse.Hit.p).magnitude <= 10 then

if mouse.Target.Occupied == false then
To this
game:GetService("RunService").RenderStepped:connect(function()

if mouse.Target and mouse.Target.Name == 'ControllerB' and (Player.Character.UpperTorso.Position - mouse.Hit.p).magnitude <= 10 then

if mouse.Target.Occupied == false then

Am I doing the “hit” part correctly, I want it so if you click on “ControllerB” it will fire this.

Then you don’t need any of this. You’re code was checking if the player’s mouse was hovering over the part, not clicking. Simply insert a ClickDetector into ControllerB, set up a function instead of renderstepped so that whenever it’s clicked the code runs.

local Player = game.Players.LocalPlayer
local Character = Player.Character
local mouse = Player:GetMouse()
local Hum = Character:WaitForChild("Humanoid")
local Tool = game.ReplicatedStorage.Controller:Clone()
local chicken = game.Workspace.ControllerB.Chinny:Clone()

local ControllerB = workspace.ControllerB -- Set this to the location of ControllerB

ControllerB.ClickDetector.MouseClick:connect(function()
   if ControllerB.Occupied then return end -- stops if occupied isnt false
   Tool.Parent = Character
   Hum:EquipTool(Tool)
   game.Workspace.Couch.CanCollide = false
   chicken.Parent = Player.PlayerGui
   game.ReplicatedStorage.PlayGame2:FireServer()
end)

But I would like it to be a mouse target click, not a click detector.

Could you please elaborate on what you mean by mouse target click? A click detector accounts for whether the object is the mouses target or not.

So basically, I would like it so you can hover over the object, and click on it, and fire all my code, instead of having a click detector do it.

Alright, so then rather than doing the above code, you can just replace the RenderStepped function with a function that checks whenever the mouse clicks, before checking whether it’s hovering over the desired target.

local Player = game.Players.LocalPlayer
local Character = Player.Character
local mouse = Player:GetMouse()
local Hum = Character:WaitForChild("Humanoid")
local Tool = game.ReplicatedStorage.Controller:Clone()
local chicken = game.Workspace.ControllerB.Chinny:Clone()

mouse.Button1Down:connect(function()
    if mouse.Target and mouse.Target.Name == 'ControllerB' and (Player.Character.UpperTorso.Position - mouse.Hit.p).magnitude <= 10 then
       if mouse.Target.Occupied then return end -- stop code if it isnt false
       Tool.Parent = Character
       Hum:EquipTool(Tool)
       game.Workspace.Couch.CanCollide = false
       chicken.Parent = Player.PlayerGui
       game.ReplicatedStorage.PlayGame2:FireServer()
    end
end)

Also, it might be a better idea to check for the HumanoidRootPart’s position rather than the UpperTorso, in instances where you have to change the character type. (Haven’t put that in the code snippet above, self-explanatory really! :slight_smile:

Is this code correct? It hasn’t worked.