I want to make a local script for for everybody (THIS IS COMPLICATED)

Hello fellow developers, I am currently having some issues regarding a Local script. I am not a great scripter so I could use some help from someone with a fresh mind unlike me.

I have a folder with a bunch random sounds that plays after every message that a player types, and I want the WHOLE server to hear these sounds. Not just the local player.

214e436ce6a4689c96e805ffb7f80f03
Here is a screenshot of the script and the explorer (I include this picture because it might be helpful for some people that actually knows what I’m trying to accomplish)

I’ve looked around for some solutions online but no one is having the same issue as me (Sad face)

local Replicated = game:GetService("ReplicatedStorage")

local Player = game:GetService("Players").LocalPlayer

Player.Chatted:Connect(function(message: string)
	local randomMusic =  Replicated.Sounds:GetChildren()[math.random(1,# Replicated.Sounds:GetChildren())]
	randomMusic:Play() 	
	end)

I am also including the script as a lua format above because i thought that would look professional.

Thank you for being patient with me.

You can use RemoteEvents.

  1. Add a remote event into ReplicatedStorage:
    image

  2. Add a script into ServerScriptService:
    image

  3. In the script paste this:

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local RemoteEvent = ReplicatedStorage.RemoteEvent
local Sounds = ReplicatedStorage.Sounds

--//Functions
Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function()
		local randomSound =  Sounds:GetChildren()[Random.new():NextInteger(1, #Sounds:GetChildren())]
		RemoteEvent:FireAllClients(randomSound)
	end)
end)
  1. The final step is to change your LocalScript to this:
--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local RemoteEvent = ReplicatedStorage.RemoteEvent

--//Functions
RemoteEvent.OnClientEvent:Connect(function(sound)
	sound:Play()
end)

This will send an event to all clients every time someone says something, which will play the sound locally for all players.

1 Like

THANK YOU. This works perfect, I greatly appreciate it!

local Sounds = ReplicatedStorage.Sounds
Sounds = Sounds:GetChildren()

--//Functions
Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function()
		local randomSound =  Sounds[Random.new():NextInteger(1, #Sounds)]
		RemoteEvent:FireAllClients(randomSound)
	end)
end)