EirikFA
|
  |
| Joined: 15 Mar 2013 |
| Total Posts: 35 |
|
|
| 09 Apr 2016 11:42 AM |
So basically I want to exit the Tool.Activated function when the player using the tool presses U. But if I try to use InputService to check if U is pressed and use return, it won't work, I assume that is because it only exits the InputService function.
Heres the script:
local Player = game.Players.LocalPlayer local mouse = Player:GetMouse() local Tool = script.Parent.Parent
Tool.Activated:connect(function() local PlayerClicked = game.Players:GetPlayerFromCharacter(mouse.Target.Parent) if PlayerClicked ~= nil then local Torso = PlayerClicked.Character.Torso if ((Torso.Position - Player.Character.Torso.Position).magnitude <= 40) then Tool.Activated:connect(function() local moveTo = mouse.Hit.p PlayerClicked.Character.Humanoid:MoveTo(moveTo) end) end end end)
How would I exit the function when I press U? |
|
|
| Report Abuse |
|
|
EirikFA
|
  |
| Joined: 15 Mar 2013 |
| Total Posts: 35 |
|
| |
|
EirikFA
|
  |
| Joined: 15 Mar 2013 |
| Total Posts: 35 |
|
| |
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 15 Apr 2016 08:29 AM |
local uis = game:GetService("UserInputService")
uis.InputBegan:connect(function(input) if input.KeyCode == Enum.KeyCode.U then print("Pressing U") end end)
|
|
|
| Report Abuse |
|
|
EirikFA
|
  |
| Joined: 15 Mar 2013 |
| Total Posts: 35 |
|
|
| 17 Apr 2016 02:35 AM |
| @TimeTicks: I know how to bind a key, but my question was how to exit the function when a key is pressed. |
|
|
| Report Abuse |
|
|
EirikFA
|
  |
| Joined: 15 Mar 2013 |
| Total Posts: 35 |
|
| |
|
EirikFA
|
  |
| Joined: 15 Mar 2013 |
| Total Posts: 35 |
|
| |
|
Solotaire
|
  |
| Joined: 30 Jul 2009 |
| Total Posts: 30356 |
|
|
| 26 Apr 2016 09:48 AM |
| I'm missing the lack of core loop in the program, what are you trying to exit? Should the player's character stop moving towards the point? |
|
|
| Report Abuse |
|
|
EirikFA
|
  |
| Joined: 15 Mar 2013 |
| Total Posts: 35 |
|
|
| 01 May 2016 07:13 AM |
@Solotaire: The point of the program is: A player that has the tool in his/her backpack, can equip it and click on other players and then click at a point where you want to move their character. So basically: The tool will control another players character.
I want the user with the tool have the ability to "unselect" the players that he/she is controlling. And therefore, I need to exit the function when a user "unselects". |
|
|
| Report Abuse |
|
|
|
| 01 May 2016 08:48 AM |
you have an activated event inside of an activated event
|
|
|
| Report Abuse |
|
|
EirikFA
|
  |
| Joined: 15 Mar 2013 |
| Total Posts: 35 |
|
| |
|
LockFocus
|
  |
| Joined: 31 Aug 2013 |
| Total Posts: 1044 |
|
|
| 11 May 2016 08:27 AM |
http://wiki.roblox.com/index.php?title=RBXScriptSignal&redirect=no#Methods
RBXScriptConnection:disconnect() |
|
|
| Report Abuse |
|
|
LockFocus
|
  |
| Joined: 31 Aug 2013 |
| Total Posts: 1044 |
|
|
| 11 May 2016 08:31 AM |
local uis = game:GetService("UserInputService") local tool
local connection = tool.Activated:connect(function() -- stuff here end)
uis.InputBegan:connect(function(input) if input.KeyCode == Enum.KeyCode.U then connection:disconnect() end end)
|
|
|
| Report Abuse |
|
|
EirikFA
|
  |
| Joined: 15 Mar 2013 |
| Total Posts: 35 |
|
|
| 28 Jun 2016 07:43 AM |
Sorry for the late reply, haven't scripted in a while.
Thanks for the tip, this is what I currently have:
-- Variables/locals.
local Player = game.Players.LocalPlayer local mouse = Player:GetMouse() local Tool = script.Parent.Parent local connection
connection = Tool.Activated:connect(function() -- When tool is Activated. local PlayerClicked = game.Players:GetPlayerFromCharacter(mouse.Target.Parent) if PlayerClicked ~= nil then local Torso = PlayerClicked.Character.Torso if ((Torso.Position - Player.Character.Torso.Position).magnitude <= 40) then Tool.Activated:connect(function() local moveTo = mouse.Hit.p PlayerClicked.Character.Humanoid:MoveTo(moveTo) game:getService("UserInputService").InputBegan:connect(function(input) if input.KeyCode == Enum.KeyCode.U then print("Input function entered.") connection:disconnect() end end) end) end end end)
"Input function entered." is printed, but disconnect() is apparently not working, nothing else happens when pressing U. |
|
|
| Report Abuse |
|
|
BanTech
|
  |
| Joined: 31 Dec 2015 |
| Total Posts: 886 |
|
|
| 28 Jun 2016 07:47 AM |
| as recurring already said: you have an activated event inside of the outer activated event. I'm pretty sure you don't mean to do that because both events will be fired when you activate the tool. |
|
|
| Report Abuse |
|
|
EirikFA
|
  |
| Joined: 15 Mar 2013 |
| Total Posts: 35 |
|
|
| 28 Jun 2016 08:12 AM |
@BanTech Theres a reason I do that:
First you click the player you want to control, then you click where you want the player to move. |
|
|
| Report Abuse |
|
|
EirikFA
|
  |
| Joined: 15 Mar 2013 |
| Total Posts: 35 |
|
|
| 28 Jun 2016 08:15 AM |
| I disconnected the second Activated event when the user presses U, which worked. Theres only one problem though: The tool is not working for the rest of its lifetime. xD |
|
|
| Report Abuse |
|
|
EirikFA
|
  |
| Joined: 15 Mar 2013 |
| Total Posts: 35 |
|
|
| 28 Jun 2016 08:18 AM |
I don't need help anymore, figured it out.
I disconnected both activated events when the user press U, and cloned the tool, removed the old one and placed the clone inside the players backpack to make a new tool that works.
Here's the finished script:
-- Variables/locals.
local Player = game.Players.LocalPlayer local mouse = Player:GetMouse() local Tool = script.Parent.Parent local connection local connection2
connection = Tool.Activated:connect(function() -- When tool is Activated. local PlayerClicked = game.Players:GetPlayerFromCharacter(mouse.Target.Parent) if PlayerClicked ~= nil then local Torso = PlayerClicked.Character.Torso if ((Torso.Position - Player.Character.Torso.Position).magnitude <= 40) then connection2 = Tool.Activated:connect(function() local moveTo = mouse.Hit.p PlayerClicked.Character.Humanoid:MoveTo(moveTo) game:getService("UserInputService").InputBegan:connect(function(input) if input.KeyCode == Enum.KeyCode.U then print("lol") connection2:disconnect() connection:disconnect() local toolClone = Tool:Clone() Tool:Remove() toolClone.Parent = Player.Backpack end end) end) end end end) |
|
|
| Report Abuse |
|
|