Lua audio script for roblox isnt working? - lua

my friend made a script for a club game and the audio seems to not work please help
local playlist = {4773093598, 727844285}
local mubic = game.Workspace.moosesack
local G_egg = true
while G_egg == true do
for i, v in ipairs(playlist) do
mubic.SoundId = v
mubic:Play()
wait(mubic.TimeLength)
end
end

Why are you trying to assign multiple Audio ID’s to a single Sound Instance at one time?
To create a functioning music playlist, you would need to create a queue.
And also, because you are using a while loop you are exhausting the Lua thread processor, you would need to add wait().
Furthermore, the while loop is an actual loop, so it will execute the same code infinitely until your boolean switches to false.

From the information that you've given ("seems to not work"), there is 1 error in your code.
mubic.SoundId = v
What the code is being read as is "mubic.SoundId = 4773093598"
To roblox, this is not correct as the SoundId must be defined as a string and as an asset id. Your code should look like:
local playlist = {4773093598, 727844285}
local mubic = game.Workspace.moosesack
local G_egg = true
while G_egg == true do
for i, v in ipairs(playlist) do
mubic.SoundId = "rbxassetid://"..tostring(v)
mubic:Play()
wait(mubic.TimeLength)
end
end

Related

Why do keep getting this error, I'm trying to turn a character invisible when the function runs

local camera = workspace.CurrentCamera
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local startergui = game:GetService("StarterGui")
local char = Players.LocalPlayer.Character
local model = workspace.OceanVillagedr201["Wine Cellar"].WineDigitalkeypad
local screen = model.screen
local replicated_storage = game:GetService("ReplicatedStorage")
local CheckCode = Instance.new("RemoteEvent")
CheckCode.Name = "CheckWineCellarCodeEvent"
CheckCode.Parent = replicated_storage
local function Entercode(player)
Players.LocalPlayer.Character.Humanoid.RootPart.Anchored = true
for _,p in pairs(char:GetChildren()) do
p.Transparency = 1
end
game.StarterGui = replicated_storage.EnterWineCellarCode
end
screen.ProximityPrompt.Triggered:Connect(function(player)
Entercode()
end)
Im attempting to create a function that triggers when the proximity prompt is triggered, One piece of this Entercode() function is to toggle the playermodel's Transparency from 0 to 1 and Remove the Characters ability to move.
local function Entercode(player)
print("went")
Players.LocalPlayer.Character.Humanoid.RootPart.Anchored = true
for _,p in pairs(char:GetChildren()) do
p.Transparency = 1
end
But Im having trouble with this Piece. It keeps telling me "attempt to index nil" with anything dealing with trying to reference the Characters model. (FFC(), GetChildren(), Player.LocalPlayer.Character, etc.). I am using a local script because I plan to create a Remote Function for the result of EnterCode()
I believe your problem is that the character either was not spawned at the time of making char or is an old character (player respawned & a new character was made). A quick fix would be to redeclare char inside Entercode:
local function Entercode()
char = player.Character
char.Humanoid.RootPart.Anchored = true
for _, p in pairs(char:GetChildren()) do
if p:IsA("BasePart") then
p.Transparency = 1
end
end
replicated_storage.EnterWineCellarCode.Parent = player.PlayerGui
end
There were also a number of other errors, here are a few changes I made:
local function Entercode()
In the original code, player was a parameter but was not sent as an argument Good thing you set the player variable at the beginning of the code.
if p:IsA("BasePart") then
p.Transparency = 1
end
In the original code, you didn't check to see if p was a part or not.
replicated_storage.EnterWineCellarCode.Parent = player.PlayerGui
In the original code, you tried to set StarterGui to EnterWineCellarCode? I don't know what you were going for but I'm assuming you meant to parent EnterWineCellarCode to PlayerGui
Lastly, you might want to use GetDescendants() instead of GetChildren(). To better understand how the character works I recommend you read the wiki entry for it

Why does my list clear/lose its contents? [ROBLOX]

local sounds = {
877986525;
2734549871;
}
local PrimaryQueue = {} -- Player Chosen Songs.
local SoundObj = workspace.MusicSystem
function PlaySoundAndWait(SoundId)
SoundObj.SoundId = "rbxassetid://"..SoundId
print("Loading Sound...")
repeat wait() until SoundObj.IsLoaded
print("Loaded")
SoundObj:Play()
repeat wait() until not SoundObj.Playing -- Wait till over.
end
local PlaySecondary = sounds
while wait(0.1) do
if #PrimaryQueue ~= 0 then
print("Play primary disregard current")
-- Play primary, disregard current.
PlaySoundAndWait(PrimaryQueue[1])
table.remove(PrimaryQueue,1) -- Remove from queue (played)
else
-- Refill Secondary Queue if empty.
if #PlaySecondary == 0 then
print("REFILL")
PlaySecondary = sounds
print(#sounds)
continue
end
print(PlaySecondary[1])
PlaySoundAndWait(PlaySecondary[1])
table.remove(PlaySecondary,1)
end
end
When I refer to "REFILL" I mean line 26 where the list is refreshed.
This script indefinitely checks if there's anything in the PrimaryQueue, if there is it plays that then removes it. If there's not it checks if the SecondaryQueue is empty, if so it refills it with "sounds". If it's not it plays the first sound then removes it.
As a result all of this should create a music system, but for some reason when refilling, the sound list reads as empty. Even though it shouldn't be and has only been assigned a value once.
Thank you.
You are doing table.remove(PlaySecondary, 1) which is equal to table.remove(sounds, 1) as they both point to the same table due to PlaySecondary = sounds, so it's coming back empty because you yourself removed all its elements previously!
I assume you wanted to create a copy of the table:
PlaySecondary = {unpack(sounds)}

When player interacts with proximity prompt it checks if the player has a tool, if not then it does a function -ROBLOX STUDIO

I'm making a gui that shows up when a player interacts with a proximity prompt, but, i want the script to check if the player has the tool in his inventory. If it has the tool then do nothing (don'
t show the gui), if it doesn't have the tool then fire an event. I tried making it but this error keeps showing up Workspace.Part.Script:6: attempt to index nil with 'Backpack'
Here's the script:
debounce = true
script.Parent.ProximityPrompt.Triggered:Connect(function(player)
if debounce then
debounce = false
local noob = game.Players:GetPlayerFromCharacter(player.Parent)
local Tool = noob.Backpack:FindFirstChild("Gunball")
if Tool == nil then
game.ReplicatedStorage.RemoteEvent:FireClient(player)
debounce = true
end
end
end)
Here's the gui script (local), even if i don't really think that is usefull..:
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
script.Parent.Visible = true
end)
Workspace.Part.Script:6: attempt to index nil with 'Backpack'
local Tool = noob.Backpack:FindFirstChild("Gunball")
Here noob is nil.
So in local noob = game.Players:GetPlayerFromCharacter(player.Parent) game.Players:GetPlayerFromCharacter(player.Parent) returns nil.
According to the Roblox documentation
This function returns the Player associated with the given
Player.Character, or nil if one cannot be found. It is equivalent to
the following function:
local function getPlayerFromCharacter(character)
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
if player.Character == character then
return player
end
end
end
So it seems that the Parent of player, player.Parent is not a Character associated with any player.
Why should a player property like Character be a parent of a player? I'm no Roblox expert but that doesn't seem to make any sense to me.
If you want to check wether the player who triggered the ProximitPrompt has some item, why not work with player? I mean that's the player who triggered it. So check its backpack, not some parent character stuff.

How do I use file.Write and file.Read to save player data

Im trying to make an experience script. The only thing I can't seem to figure is Data Saving. Im using file.Write and whenever I have my script read the player's level and experience from the data, it doesnt show up. I have my variables xp equal to 0 and level equal to 1. If I wanted to save the number I assigned or any number added to this to equal a new one to a txt file using file.Write as well as having that data read whenever a player spawns, how could I accomplish this?
local xp = 0
local level = 1
local players = player.GetAll()
for k ,v in pairs(players) do
file.Write("xpdata.txt", xp)
file.Write("leveldata.txt", level)
end
hook.Add("PlayerSpawn", "leveldata", function()
file.Read("xpdata.txt", "DATA")
file.Read("leveldata.txt", "DATA")
end)
I think you need to use json.
I can't write now a whole example but you can have a look at this page https://wiki.gideros.rocks/index.php/Start_Here#Save.2Fread_data_persistently
maybe that can help you understand the concept.
What do you expect to happen if you just read a file into nirvana?
hook.Add("PlayerSpawn", "leveldata", function()
file.Read("xpdata.txt", "DATA")
file.Read("leveldata.txt", "DATA")
end)
Here you read two files but you don't do anything with the return values.
file.Read returns a string with the files contents if successful.
You need to do something with the return value.
local xpData = file.Read("xpdata.txt", "DATA")
if not xpData then
print("Reading data/xpdata.txt failed!")
else
print("XP: ", xpData)
end
Of course you should store that data per player and make sure each player gets its own xp assigned.
local xp = 0
local level = 1
local players = player.GetAll()
for k ,v in pairs(players) do
file.Write("xpdata.txt", xp)
file.Write("leveldata.txt", level)
end
This doesn't make too much sense as well.
You're writing to the same file for every player. Each loop cycle will overwrite the file of the cycle before.
Also having the same xp and level for each player won't help much.
So either you have one file per player or you have some kind of structure in that common file that allows you to write and read player specific data.
You need to "serialize" your data befor you write it to the file and deserialize it later.
This can be achieved by using two utility functions
https://wiki.facepunch.com/gmod/util.TableToJSON
https://wiki.facepunch.com/gmod/util.JSONToTable
Then you can do something like this
local playerData = {}
-- for each player
for k, player in pairs(players) do
-- add a table with xp and lvl so you can later access it by AccountID
playerData[player.AccountID] = {xp = player.xp, lvl = player.lvl}
end
local jsonStr = util.TableToJSON(playerData)
file.Write("playerdata.xp", jsonStr")
I'll leave the rest up to you so you can learn something.

attempt to index nil with 'leaderstats' in roblox studio

local garbage = game.Teams["Glizzy Garbage"]
local player = game.Players.LocalPlayer
if player.leaderstats.Pounds.Value <= 1000 then --this is the line that the output is detecting the error
player.Team = garbage
end
I am trying to make it where when the player reaches a certain amount of 'pounds' then they will automatically receive a roll. I've searched through many youtube videos and haven't found a fix or an alternative way to do this, and I'm not sure why this isn't working. This script is located in the workspace. All help is appreciated.
The LocalPlayer object is only exposed in LocalScripts. Because you're using a Script in the Workspace, you'll have to access the player object another way. It's also a good idea to handle this kind of logic inside a function that is fired any time the Pounds value changes.
Try using the game.Players.PlayerAdded signal :
local garbage = game.Teams["Glizzy Garbage"]
game.Players.PlayerAdded:Connect(function(player)
local Pounds = player.leaderstats.Pounds
Pounds.Changed:Connect(function(value)
if value <= 1000 then
player.Team = garbage
end
end)
end)
Mine is just
local plrStage = plr.leaderstats.Stage.Value
try instead of putting team name put team colour
also use
player.Team.Service = garbage
end)

Resources