As you can see in the video, this button is pretty unreliable. Like sometimes i would step on it and it would activate, and then deactivate. And sometimes when i step off the button it would stay activated. How do i fix this, and make it more reliable? Also some feedback would be great too. Thanks!
BoolValueObject = script.Parent:WaitForChild("BoolValueObject")
Button = script.Parent.PrimaryPart
Sound = Button:WaitForChild("ButtonSound")
ButtonIncrement = 0.2
debounce = false
canToggle = true
function Activate()
if not debounce and canToggle then
debounce = true
canToggle = false
Button.Position = Button.Position - Vector3.new(0, ButtonIncrement, 0)
local Bool = BoolValueObject.Value
Sound:Play()
--Bool.Value = true
wait(0.2)
canToggle = true
end
end
function Deactivate()
if debounce and canToggle then
debounce = false
canToggle = false
Button.Position = Button.Position + Vector3.new(0, ButtonIncrement, 0)
local Bool = BoolValueObject.Value
Sound:Play()
--Bool.Value = false
wait(0.2)
canToggle = true
end
end
Button.Touched:Connect(Activate)
Button.TouchEnded:Connect(Deactivate)
Be sure to check out Instance | Documentation - Roblox Creator Hub. Humanoid floor type or whatever. Humanoid state (if they’re freefalling they won’t be on anything, so no need to check), and one RenderStep raycast from the HumanoidRootPart down tied to everything that might need it would seem to do the trick.
I’ve never actually tried this, but I theorized it.
Touch events are fired whenever something touches a part, then touch ended fires whenever something leaves the part.
Sometimes touch events don’t register all the touch events.
My theory to fix this problem is to not only check if the touched parts parent has a humanoid, but also check for a specific body part, for example, the right foot.
So the code would look something like this:
if hit.Parent:FindFirstChild("Humanoid") and hit.Name == "RightFoot" then
BoolValueObject = script.Parent:WaitForChild("BoolValueObject")
Button = script.Parent.PrimaryPart
Sound = Button:WaitForChild("ButtonSound")
ButtonIncrement = 0.3
debounce = script.Parent:WaitForChild("debounce")
function Activate()
if debounce.Value == false then
debounce.Value = true
Button.Position = Button.Position - Vector3.new(0, ButtonIncrement, 0)
local Bool = BoolValueObject.Value
Sound:Play()
Bool.Value = true
end
end
function Deactivate()
if debounce.Value == true then
debounce.Value = false
Button.Position = Button.Position + Vector3.new(0, ButtonIncrement, 0)
local Bool = BoolValueObject.Value
Sound:Play()
Bool.Value = false
end
end
while true do
wait()
local function GetTouchingParts(part)
local connection = part.Touched:Connect(function() end)
local results = part:GetTouchingParts()
connection:Disconnect()
return results
end
local results = GetTouchingParts(Button)
local count = 0
for i, child in ipairs(results) do
if child.Parent ~= script.Parent then
count = count + 1
end
end
print(count)
if count > 0 then
Activate()
else
Deactivate()
end
end