generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Exit function when a key is pressed

Previous Thread :: Next Thread 
EirikFA is not online. 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 is not online. EirikFA
Joined: 15 Mar 2013
Total Posts: 35
09 Apr 2016 12:18 PM
Bump
Report Abuse
EirikFA is not online. EirikFA
Joined: 15 Mar 2013
Total Posts: 35
15 Apr 2016 05:14 AM
Bump
Report Abuse
TimeTicks is not online. 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 is not online. 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 is not online. EirikFA
Joined: 15 Mar 2013
Total Posts: 35
18 Apr 2016 11:41 AM
Bump
Report Abuse
EirikFA is not online. EirikFA
Joined: 15 Mar 2013
Total Posts: 35
26 Apr 2016 09:40 AM
Bump
Report Abuse
Solotaire is not online. 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 is not online. 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
RecurringNightmare is not online. RecurringNightmare
Joined: 05 Jul 2012
Total Posts: 15336
01 May 2016 08:48 AM
you have an activated event inside of an activated event


Report Abuse
EirikFA is not online. EirikFA
Joined: 15 Mar 2013
Total Posts: 35
11 May 2016 07:41 AM
Anyone? Bump
Report Abuse
LockFocus is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image