Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why doesn't this Key down function work?

Asked by
Mystdar 352 Moderation Voter
9 years ago

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 fromgame.Players.LocalPlayer, instead of game.Workspace[playername] should mouse (variable) be:

local mouse = game.Workspace[playername]:GetMouse()
0
Not sure, but maybe try using the binding function from the ContextActionService tutorial? http://wiki.roblox.com/index.php?title=ContextActionService_tutorial Spongocardo 1991 — 9y
0
Seems slightly too advanced for my level. Mystdar 352 — 9y
0
I would first add a 'print' before the KeyDown function to make sure that "MagicEquipped" is found in the player. RedCombee 585 — 9y
0
Sorry, i'm a printing noob, how would I go about that? Mystdar 352 — 9y
View all comments (2 more)
0
game.Players.LocalPlayer:GetMouse() is correct, don't worry. Redbullusa 1580 — 9y
0
Damn, something is wrong, because it doesn't work, just can't find what. Mystdar 352 — 9y

4 answers

Log in to vote
0
Answered by 9 years ago

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

0
Tried that, still nothing. Mystdar 352 — 9y
0
Please tell me the classname of Magicequiped SilenceCore 25 — 9y
0
Bool Mystdar 352 — 9y
Ad
Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

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.

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. Mystdar 352 — 9y
0
Still nothing Mystdar 352 — 9y
0
What does the output say? Also, can you try and relocate "MagicEquiped?" If you don't want to, then read my edit. Redbullusa 1580 — 9y
0
No output help, afraid I am completely baffled to why this doesn't work Mystdar 352 — 9y
View all comments (3 more)
0
:( Are you using a regular script? Redbullusa 1580 — 9y
0
No, local. Mystdar 352 — 9y
0
Hrm. Did you add any statements after your 'if' statement? Maybe the error originated from there. Redbullusa 1580 — 9y
Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

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);
    
Log in to vote
-2
Answered by 9 years ago

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)
0
Wait, your using that for mouse click, but you also have a keyboard button for the same purpose too? SprocketSoldier 0 — 9y
0
No, I just want the function to execute when they press q Mystdar 352 — 9y
0
Still nothing Mystdar 352 — 9y
0
Try mine, you forgot to add game.Players.LocalPlayer to the button down SprocketSoldier 0 — 9y
View all comments (2 more)
0
Tch...you gave me a -1? I'm sitting here trying to help you out SprocketSoldier 0 — 9y
0
I didn't give you the Minus one. Mystdar 352 — 9y

Answer this question