lplayer.Chatted:Connect(function(msg)
if string.sub(msg, 1, 7) == (prefix.."music ") then
end
end)
so I get up to this point then I don't know what to do where it gets the ID from you saying :music [ID} then it plays. Anybody know?
lplayer.Chatted:Connect(function(msg)
if string.sub(msg, 1, 7) == (prefix.."music ") then
end
end)
so I get up to this point then I don't know what to do where it gets the ID from you saying :music [ID} then it plays. Anybody know?
Use gsub to replace your prexfix and "music " with "", that will leave you with just the ID. From there you can create a sound in workspace with the ID and play it
example script:
game.Players.PlayerAdded:connect(function(Player)
Player.Chatted:connect(function(Message)
if string.sub(Message, 1,7) == ":music " then
local ID = string.gsub(Message, ":music ", "")
if workspace:FindFirstChild("Sound") then
workspace.Sound:Remove()
end
local Sound = Instance.new("Sound", workspace)
Sound.SoundId = "rbxassetid://"..ID
Sound.Looped = true
Sound:Play()
end
end)
end)
Since you want to do this from a local script, you won't be able to create a new sound instance for everyone, so no one else will be able to hear the music. So, what you need to do is use remote events:
your local script:
local RE = game.ReplicatedStorage:WaitForChild("MusicEvent")
local localPlayer = game.Players.LocalPlayer
localPlayer.Chatted:connect(function(Message)
if string.sub(Message, 1,7) == ":music " then
local ID = string.gsub(Message, ":music ", "")
RE:FireServer(ID)
end
end)
Server script inside of server script service:
local RE = Instance.new("RemoteEvent")
RE.Parent = game.ReplicatedStorage
RE.Name = "MusicEvent"
RE.OnServerEvent:Connect(function(player, musicID)
if workspace:FindFirstChild("Sound") then
local Sound = workspace.Sound
Sound:Stop()
Sound.SoundId = "rbxassetid://"..musicID
Sound.Looped = true
Sound:Play()
else
local Sound = Instance.new("Sound")
Sound.SoundId = "rbxassetid://"..musicID
Sound.Looped = true
Sound.Parent = workspace
Sound:Play()
end
end)
If this answers your question then mark it as the accepted answer. I can also clear up any further questions you might have about this.
I made it so it's possible for you to add other commands, if "music" is not where you're going to stop at
local Players = game:GetService("Players");
local admins = {"tantec"};
local sound = Instance.new("Sound");
sound.Parent = workspace;
local prefix = ":";
local commands = {
music = function(id)
sound.SoundId = tonumber(id);
sound:Play();
end;
};
for i,v in pairs(Players:GetPlayers()) do
local check = false;
for a,b in pairs(admins) do
if v.Name == b then
check = true;
end;
end;
if check == false then continue end;
v.Chatted:Connect(function(msg)
for c = 1,#msg do
if msg:sub(c,c) == prefix then
local tab = string.split(msg:sub(c+1, #msg));
local cmd = tab[1]:lower();
local arg = tab[2];
pcall(function()
commands[cmd](arg);
end);
end;
end;
end);
end;
This discussion is preserved as it was. New answers, comments, and votes are closed.