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

How do i make this script toggle on and off with a keypress?

Asked by 1 year ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

I am making an obby game where people will have to change the physics of the ground at certain times to be slippery so they can do certain obstacles but I am unsure how to make this script toggle on and off with a keypress for i, v in pairs(workspace:GetDescendants()) do if v:IsA("BasePart") then v.CustomPhysicalProperties = PhysicalProperties.new(0.05, 0, 0.5, 50, 1)

    end

end.

1 answer

Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
1 year ago
Edited 1 year ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Here you go

To make this we use UserInputService to check if they pressed a button, we also make a variable called toggeled so we know if it's on or off, then when we pressed E or whatever button you want we look through all of the descendants/stuff inside workspace, if the toggled is false and the we find a part, we change the customphysicalproperties else if the toggeled is already true then we revert back to normal

local UIS = game:GetService("UserInputService")

local keyCode = "E" -- Set the key you want to set

local toggeled = false

UIS.InputBegan:Connect(function(key, gameProcessed)
    if not gameProcessed then
        if key.KeyCode == Enum.KeyCode[keyCode] then

            for i, v in pairs(workspace:GetDescendants()) do
                if not toggeled then

                    if v:IsA("BasePart") then
                        v.CustomPhysicalProperties = PhysicalProperties.new(0.05, 0, 0.5, 50, 1)
                    end

                    toggeled = true

                else
                    if v:IsA("BasePart") then
                        v.CustomPhysicalProperties = PhysicalProperties.new(0.05, 0, 0.5, 50, 1) -- Replace to default
                    end

                    toggeled = false

                end
            end

        end
    end
end)
Ad

Answer this question