Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.