Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

if I were to make a music command how do I make it work?

Asked by 4 years ago
1lplayer.Chatted:Connect(function(msg)
2if string.sub(msg, 1, 7) == (prefix.."music ") then
3 
4end
5end)

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?

3 answers

Log in to vote
0
Answered by
Robowon1 323 Moderation Voter
4 years ago

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:

01game.Players.PlayerAdded:connect(function(Player)
02    Player.Chatted:connect(function(Message)
03        if string.sub(Message, 1,7) == ":music " then
04            local ID = string.gsub(Message, ":music ", "")
05            if workspace:FindFirstChild("Sound") then
06                workspace.Sound:Remove()
07            end
08            local Sound = Instance.new("Sound", workspace)
09            Sound.SoundId = "rbxassetid://"..ID
10            Sound.Looped = true
11            Sound:Play()
12 
13        end
14    end)
15end)
Ad
Log in to vote
0
Answered by
SteamG00B 1633 Moderation Voter
4 years ago
Edited 4 years ago

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:

1local RE = game.ReplicatedStorage:WaitForChild("MusicEvent")
2local localPlayer = game.Players.LocalPlayer
3 
4localPlayer.Chatted:connect(function(Message)
5    if string.sub(Message, 1,7) == ":music " then
6        local ID = string.gsub(Message, ":music ", "")
7        RE:FireServer(ID)
8    end
9end)

Server script inside of server script service:

01local RE = Instance.new("RemoteEvent")
02RE.Parent = game.ReplicatedStorage
03RE.Name = "MusicEvent"
04 
05RE.OnServerEvent:Connect(function(player, musicID)
06    if workspace:FindFirstChild("Sound") then
07        local Sound = workspace.Sound
08        Sound:Stop()
09        Sound.SoundId = "rbxassetid://"..musicID
10        Sound.Looped = true
11        Sound:Play()
12    else
13        local Sound = Instance.new("Sound")
14        Sound.SoundId = "rbxassetid://"..musicID
15        Sound.Looped = true
16        Sound.Parent = workspace
17        Sound:Play()
18    end
19end)

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.

Log in to vote
0
Answered by
tantec 305 Moderation Voter
4 years ago
Edited 4 years ago

I made it so it's possible for you to add other commands, if "music" is not where you're going to stop at

01local Players = game:GetService("Players");
02local admins = {"tantec"};
03local sound = Instance.new("Sound");
04sound.Parent = workspace;
05local prefix = ":";
06local commands = {
07    music = function(id)
08        sound.SoundId = tonumber(id);
09        sound:Play();
10    end;
11};
12 
13for i,v in pairs(Players:GetPlayers()) do
14    local check = false;
15    for a,b in pairs(admins) do
View all 33 lines...

Answer this question