Advertisement
MrFinn

Turtle - Playsound on redstone signal

Nov 14th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. -- Script: play_cogs_on_any_redstone_event.lua
  2. -- Description: Plays the "create:cogs" sound when any redstone signal is detected using event-based approach.
  3.  
  4. local speaker = peripheral.wrap("left") -- Wraps the speaker on the left side
  5.  
  6. -- Verify if the speaker peripheral exists
  7. if not speaker then
  8. print("Error: No speaker found on the left side!")
  9. return
  10. end
  11.  
  12. print("Waiting for redstone signal on any side...")
  13.  
  14. -- Function to check if any side has an active redstone signal
  15. local function isRedstoneOn()
  16. local sides = {"top", "bottom", "left", "right", "front", "back"}
  17. for _, side in ipairs(sides) do
  18. if redstone.getInput(side) then
  19. return true
  20. end
  21. end
  22. return false
  23. end
  24.  
  25. while true do
  26. -- Wait for a redstone event (triggers only on redstone state change)
  27. os.pullEvent("redstone")
  28.  
  29. -- Check if there’s an active redstone signal on any side
  30. if isRedstoneOn() then
  31. -- Play the 'create:cogs' sound at volume 1 and pitch 1
  32. speaker.playSound("create:cogs", 0.4, 2)
  33. print("Playing sound: create:cogs")
  34.  
  35. -- Optional: Wait a bit before accepting new signals (e.g., 1 second)
  36. sleep(1)
  37. end
  38. end
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement