I’m creating a game where a specific area switches the client’s music from clientsided to a jukebox that’s playing on the server. The script works as intended, but there’s an unfortunate issue where simply moving anywhere within the part that changes the music may fire TouchEnded, even if the player is very obviously still touching the part.
Is there an error with my method or is it an issue with the touch events? If it’s an issue with the touch events, what are some alternative methods I can use for the same, albeit reliable effect?
(This is a LocalScript on PlayerScripts that transfers the part to the workspace)
local TouchPart = script:WaitForChild("TouchDetectorForMusic")
local TweenService = game:GetService("TweenService")
local InteractiveObjects = game:GetService("Workspace"):WaitForChild("Interactive Objects")
local JukeboxSound = InteractiveObjects:WaitForChild("Jukebox Soundpart"):WaitForChild("Sound")
local DefaultJukeboxVolume = JukeboxSound.Volume
JukeboxSound.Volume = 0
local player = game.Players.LocalPlayer
TouchPart.Parent = workspace
local AudioChangeCommand = player:WaitForChild("PlayerGui"):WaitForChild("MasterScript"):WaitForChild("Audio Command Client-side")
local StereoTweenOn = TweenService:Create(JukeboxSound, TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut), { Volume = DefaultJukeboxVolume })
local StereoTweenOff = TweenService:Create(JukeboxSound, TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut), { Volume = 0 })
local debounce = false
TouchPart.Touched:Connect(function()
if debounce == false then
debounce = true
AudioChangeCommand:Fire("mute")
if StereoTweenOff.PlaybackState == 2 then
StereoTweenOff:Cancel()
end
StereoTweenOn:Play()
end
end)
TouchPart.TouchEnded:Connect(function()
print("eventExit")
print(debounce)
if debounce == true then
print("eventExit passed debounce")
debounce = false
AudioChangeCommand:Fire("unmute")
if StereoTweenOn.PlaybackState == 2 then
StereoTweenOn:Cancel()
end
StereoTweenOff:Play()
end
end)