Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

adicione a funaço de dar player num sound no soundservice quando a animaçao for tocada :…

  1. User

    adicione a funaço de dar player num sound no soundservice quando a animaçao for tocada : local player = game.Players.LocalPlayer local TextBox = script.Parent -- Adjust this if the script isn't a direct child of the TextBox

    -- Replace "AnimationIDHere" with your actual animation ID local animationId = "rbxassetid://131028936257839" local Animation = Instance.new("Animation") Animation.AnimationId = animationId

    -- Variables to manage animation and input local animationTrack local keysPressed = 0 -- Counter for keys currently pressed (PC) local debounce = false local isAnimationPlaying = false

    local RunService = game:GetService("RunService") local lastTextChangeTime = nil local typingStopDelay = 0.75 -- Time after which we consider typing has stopped (mobile)

    -- RemoteEvent for accessory management local ReplicatedStorage = game:GetService("ReplicatedStorage") local AccessoryEvent = ReplicatedStorage:WaitForChild("AccessoryEvent") local lastAccessoryEventTime = 0 local accessoryEventCooldown = 0.5 -- Send typing notification at most every 0.5 seconds

    local function playAnimation() if not isAnimationPlaying then -- Play the animation local character = player.Character or player.CharacterAdded:Wait() local humanoid = character:FindFirstChildOfClass("Humanoid") if humanoid then animationTrack = humanoid:LoadAnimation(Animation) animationTrack:Play() isAnimationPlaying = true end end end

    local function stopAnimation() if isAnimationPlaying and animationTrack then animationTrack:Stop() isAnimationPlaying = false end end

    -- Function to handle input began in the TextBox (for PC) local function onInputBegan(input) if input.UserInputType == Enum.UserInputType.Keyboard then keysPressed = keysPressed + 1 if not debounce then debounce = true playAnimation()

    		-- Send typing notification to server with cooldown
    		if tick() - lastAccessoryEventTime >= accessoryEventCooldown then
    			AccessoryEvent:FireServer("Typing")
    			lastAccessoryEventTime = tick()
    		end
    
    		-- Debounce timer of 0.1 seconds
    		task.delay(0.75, function()
    			debounce = false
    		end)
    	end
    end
    

    end

    -- Function to handle input ended in the TextBox (for PC) local function onInputEnded(input) if input.UserInputType == Enum.UserInputType.Keyboard then keysPressed = math.max(keysPressed - 1, 0) if keysPressed == 0 then stopAnimation() end end end

    -- Connect for keyboard input in the TextBox (for PC) TextBox.InputBegan:Connect(onInputBegan) TextBox.InputEnded:Connect(onInputEnded)

    -- Connect to Text changes in the TextBox (for mobile) TextBox:GetPropertyChangedSignal("Text"):Connect(function() if TextBox:IsFocused() then lastTextChangeTime = tick() if not isAnimationPlaying then playAnimation() end

    	-- Send typing notification to server with cooldown
    	if tick() - lastAccessoryEventTime >= accessoryEventCooldown then
    		AccessoryEvent:FireServer("Typing")
    		lastAccessoryEventTime = tick()
    	end
    end
    

    end)

    -- Use Heartbeat to check for typing stop on mobile RunService.Heartbeat:Connect(function(step) if lastTextChangeTime and (tick() - lastTextChangeTime) >= typingStopDelay then stopAnimation() lastTextChangeTime = nil end end)

    -- Also handle focus lost to stop the animation TextBox.FocusLost:Connect(function() stopAnimation() lastTextChangeTime = nil keysPressed = 0 end)

  2. Assistant

    Claro! Para adicionar a função de reproduzir um som no SoundService quando a animação for tocada, você pode modificar a função playAnimation() no seu script. Abaixo está o código modificado com a adição da reprodução do som:

    local SoundService = game:GetService("SoundService")
    
    -- Defina o ID do som que você quer reproduzir
    local soundId = "rbxassetid://SeuSoundIDAqui" -- substitua pelo ID do seu som
    
    local function playAnimation()
    	if not isAnimationPlaying then
    		-- Play the animation
    		local character = player.Character or player.CharacterAdded:Wait()
    		local humanoid = character:FindFirstChildOfClass("Humanoid")
    		if humanoid then
    			animationTrack = humanoid:LoadAnimation(Animation)
    			animationTrack:Play()
    			isAnimationPlaying = true
    		end
    
    		-- Play the sound in SoundService
    		local sound = SoundService:FindFirstChild("TypingSound")
    		if not sound then
    			-- If the sound doesn't exist, create it
    			sound = Instance.new("Sound")
    			sound.Name = "TypingSound"
    			sound.SoundId = soundId
    			sound.Parent = SoundService
    		end
    		sound:Play()
    	end
    end
    

    Explicação dos novos trechos de código:

    1. Obtendo o SoundService:

      local SoundService = game:GetService("SoundService")
      

      Isso permite que você acesse o SoundService para reproduzir sons globalmente.

    2. Definindo o soundId:

      local soundId = "rbxassetid://SeuSoundIDAqui" -- substitua pelo ID do seu som
      

      Substitua "rbxassetid://SeuSoundIDAqui" pelo ID do som que você deseja reproduzir.

    3. Modificando a função playAnimation():

      Dentro da função, após iniciar a animação, adicionamos o código para reproduzir o som:

      -- Play the sound in SoundService
      local sound = SoundService:FindFirstChild("TypingSound")
      if not sound then
          -- If the sound doesn't exist, create it
          sound = Instance.new("Sound")
          sound.Name = "TypingSound"
          sound.SoundId = soundId
          sound.Parent = SoundService
      end
      sound:Play()
      

      O que este código faz:

      • Verifica se já existe um som chamado "TypingSound" no SoundService.
      • Se não existir, cria um novo Sound, define seu SoundId, nomeia-o como "TypingSound" e o parentiza ao SoundService.
      • Então, reproduz o som usando sound:Play().

    Certifique-se de:

    • Substituir "rbxassetid://SeuSoundIDAqui" pelo ID correto do som que deseja tocar.
    • Opcionalmente, você pode ajustar propriedades adicionais do som, como Volume, Pitch, etc., conforme necessário.

    Nota Adicional:

    Se você quiser garantir que o som não se sobreponha caso a função playAnimation() seja chamada novamente antes do som terminar, você pode parar o som antes de reproduzi-lo novamente ou verificar se ele já está tocando:

    if sound.IsPlaying then
    	sound:Stop()
    end
    sound:Play()
    

    Adicione isso antes de sound:Play() para evitar sobreposições indesejadas.


    Dessa forma, sempre que a animação for tocada, o som também será reproduzido pelo SoundService.