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
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?

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:

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)
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:

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.

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

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;

Answer this question