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

How can I fix this keyboard-esque plank script?

Asked by 3 years ago
local Plank = workspace.Plank
local Numbers = Plank.Folder

local Mouse = Plrs.LocalPlayer:GetMouse()
local Dist = 10
local Mag = (Plank.Position - Dist).Magnitude

Mouse.KeyDown:Connect(function(Key)
    if Mag <= Dist then
        if Key == "A" then
            print("In range!")
        end
    else 
        print("Not in range!")
    end
end)

I'm trying to make it so that when you click this plank it plays a sound which resembles knocking on wood; it's only supposed to work when the player is within 10 units of the plank though. It seems it does not print "In range!" though. Why?

2 answers

Log in to vote
0
Answered by
valk_3D 140
3 years ago
Edited 3 years ago

you should use a click detector.

insert a click detector into the plank and set its max distance to 10 and create a script that detects the activation

script.Parent.ClickDetector.MaxActivationDistance = 10

script.Parent.ClickDetector.MouseClick:Connect(function()
    print("Clicked")
end)
Ad
Log in to vote
0
Answered by 3 years ago

Replace KeyDown on line 8 with MouseButton1Down and get rid of the key parameter. Since you want a key press too, use UserInputService. You can read about it on the dev wikia. The reason your script doesn’t work is because key doesn’t work on mouse. Let me know if this helped you.

0
Okay, I'll try that. chemistk1 7 — 3y

Answer this question