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
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.
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.
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.
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
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)
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!