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

How do i make a block spawn when i press a certain button?

Asked by 4 years ago

Alright im following multiple tutorials but i did this

function.keydown:connect(function(key))
        if (key:byte() == 48) then
            Instance.new("Part", workspace)
        end 

Im new to this stuff so i dont really understand it so any help would be useful.

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Do not use the second argument of Instance.new().

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, processed)
    if not processed then
        if input.KeyCode == Enum.KeyCode.F -- Change Keycode
            local part = Instance.new("Part")
            part.Parent = workspace -- Change the parent AFTER
        end
    end
end)
Ad
Log in to vote
1
Answered by 4 years ago

UserInputService is probably better for this case:

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.A then
        Instance.new("Part", workspace)
    end
end)
Log in to vote
0
Answered by
DesertusX 435 Moderation Voter
4 years ago

Put a local script in the TextButton and try this:

local tb = script.Parent

tb:MouseButton1Click:Connect(function()
    Instance.new("Part", workspace)
end)

Answer this question