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

Why doesn't this Key down function work?

This is all that is in the local script in the StarterGUI

wait (1)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Magic = player:WaitForChild('MagicEquiped')
local playername = player.Name 

mouse.KeyDown:connect(function(key) -- This bit here doesn't work
	if string.lower(key) == "q" then
		if Magic == true then
			Magic = false
		elseif Magic == false then
			Magic = true
		end
	end
end)

EDIT: Would it be because i'm trying to get the mouse from game.Players.LocalPlayer, instead of game.Workspace[playername] should mouse (variable) be:

local mouse = game.Workspace[playername]:GetMouse()

6 comments

0

Not sure, but maybe try using the binding function from the ContextActionService tutorial? http://wiki.roblox.com/index.php?title=ContextActionService_tutorial

0

Seems slightly too advanced for my level.

0

I would first add a 'print' before the KeyDown function to make sure that "MagicEquipped" is found in the player.

0

Sorry, i'm a printing noob, how would I go about that?

0

game.Players.LocalPlayer:GetMouse() is correct, don't worry.

0

Damn, something is wrong, because it doesn't work, just can't find what.

4 answers

Your variable magic is it a boolvalue? If it is then magic.value == false/true

3 comments

0

Tried that, still nothing.

0

Please tell me the classname of Magicequiped

0

Bool

If local Magic or MagicEquiped is a boolean value, then this should fix it:

wait (1)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Magic = player:WaitForChild('MagicEquiped')
local playername = player.Name

mouse.KeyDown:connect(function (key)
    if string.lower(key) == "q" then
        if Magic.Value == true then
		Magic.Value = false
-- Statement(s)
        elseif Magic.Value == false then
		Magic.Value = true
-- Statement(s)
        end
    end
end)

The function was meant to find the Value of the "Magic" boolean, but instead your script was coded in a way that tries to adjust its existence.

If you do want to do in the Magic = true style (without .Value, and therefore, without needing the Boolean Value), you can make a boolean value within your script.

wait (1)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local playername = player.Name
local Magic = true

mouse.KeyDown:connect(function (key)
    if string.lower(key) == "q" then
        if Magic == true then
		Magic = false
-- Statement(s)
        elseif Magic == false then
  		Magic = true
-- Statement(s)
        end
    end
end)

^^If other scripts are using MagicEquiped, then don't use this code. xD The Magic boolean in-script value is specifically for that script and that script only.

Just in case if the script can't find the player, then this is one of the good replacements for wait(1):

repeat wait() until game.Players.LocalPlayer ~= nil

EDIT

repeat wait() until game.Players.LocalPlayer ~= nil
repeat wait() until game.Players.LocalPlayer.MagicEquiped ~= nil
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Magic = player.MagicEquiped
local playername = player.Name

mouse.KeyDown:connect(function (key)
    if string.lower(key) == "q" then
        if Magic.Value == true then
            Magic.Value = false
-- Statement(s)
        elseif Magic.Value == false then
            Magic.Value = true
-- Statement(s)
        end
    end
end)

The script will now wait until your player renders. Then it will wait until the bool value is rendered.

7 comments

0

Thanks, i'll go with the first one since I intend to access it from other scripts, I will test it in the morning.

0

Still nothing

0

What does the output say? Also, can you try and relocate "MagicEquiped?" If you don't want to, then read my edit.

0

No output help, afraid I am completely baffled to why this doesn't work

0

:( Are you using a regular script?

0

No, local.

0

Hrm. Did you add any statements after your 'if' statement? Maybe the error originated from there.

Heyyo,

In order for you code to work "Magic" must be a BoolValue, which I assume it is... Also, you should be checking for Magic's Value property instead of Magic itself.

mouse.KeyDown:connect(function(key) 
    if string.lower(key) == "q" then
        if Magic.Value == true then
            Magic.Value = false
        elseif Magic.Value == false then
            Magic.Value = true
        end
    end
end)

or, much simpler..

mouse.KeyDown:connect(function(key)
    if string.lower(key) == "q" then
  Magic.Value=not Magic.Value
  end;
end);

Ok I think I get what your trying to do, and I don't believe you need the local mouse variable.

At the part where you said "this part doesn't work" try this;

game.Players.LocalPlayer:GetMouse() .KeyDown:connect (function(key)

6 comments

0

Wait, your using that for mouse click, but you also have a keyboard button for the same purpose too?

0

No, I just want the function to execute when they press q

0

Still nothing

0

Try mine, you forgot to add game.Players.LocalPlayer to the button down

0

Tch...you gave me a -1? I'm sitting here trying to help you out

0

I didn't give you the Minus one.

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