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

Pick up with q script not working???

Asked by 8 years ago
local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local target = Mouse.Target


Mouse.KeyDown:connect(function(key)
    if (key=="q") then --if q is pressed...

        Mouse.Move:connect(function(target) --...checkthe target...
            if target:FindFirstChild("SBF") then --...check if target contains stringvalue sbf...
                print("hi")--...if so print hi...

            else
                print("ew")--...if not eww.

            end
        end)
    end
end)**

As the notes in the code says. I want my script to be like on "q check if a part or tool has string-value "SBF" in it and then if so print hi if not print ew.

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

Line 3 is a common mistake. Setting mouse.Target to a variable doesn't save the property itself, only the value of the property when you access it. You have to re-access mouse.Target every time you need to know its value.

KeyDown is deprecated, so I highly suggest using the UserInputService or ContextActionService instead, both of which provide far greater functionality and far fewer bugs.

Since you only care about the mouse's Target when they press the 'q' key, so hooking up a mouse.Move event is not necessary.

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()


Mouse.KeyDown:connect(function(key)
    key = key:lower() -- so holding Shift doesn't affect anything
    if ( key == "q") then --Lua isn't C, so parentheses here are not strictly necessary.
        if Mouse.Target:FindFirstChild("SBF") then
            print("hi")
        else
            print("ew")
        end
    end
end)

Also, a side note about comments in code:

Comments should explain why, not what. That is, only use comments when the line of code you just wrote does something complex or fancy, or if the reason for it isn't immediately obvious, to explain what it is you just did.

Don't use comments to explain how an if statement works, as that is redundant.

Ad

Answer this question