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

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

Related

Lua audio script for roblox isnt working?

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

How do I make this code function correctly?

I want to make a sort of Lobby system in Roblox Studio where if you have 4 people on a part you get sent to another place. I tried to set up a system for it, but it didn't work; can you help me through this?
I've tried making it so it says .Value at the end.
local TeleportService = game:GetService("TeleportService")
player_amount = script.Parent.Parent.Parent.Player_Count
local placeID_1 = 4119652438
local function onPartTouch(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
player_amount.Value = player_amount.Value + 1
end
if player_amount == 4 then
TeleportService:Teleport(placeID_1, player)
end
end
script.Parent.Touched:Connect(onPartTouch)
I expected the output to be 0 then if one person steps on it, it would update the sign to say 1. But it only stays at 0.
This is not a viable solution as .Touched fires every frame the player touches a part, and only when they move. I suggest creating a hitbox, as I have done here

roblox studio plr.Name == ""

i'm making a script that detects a person using their name however I can't seem to get it working. Please also point out any mistakes I have done in my script it would help a lot.
game.Workspace:WaitForChild("Console")
print("Waited")
game.Players.PlayerAdded:Connect(function(plr)
print("Connected")
if game.Workspace.Console and plr.Name == "wojciechpa2007" then
local Console = game.Lighting.Console:Clone()
Console.Parent = plr.Startergui
print("Cloned")
elseif
not game.Workspace.Console and plr.Name == "wojciechpa2007" then
plr.Startergui.Console:Destroy()
print("Destroyed")
end
end)
Heyo,
This script has a race-condition in it. Your first line game.Workspace:WaitForChild("Console") will block execution of the rest of your script until the object is loaded, or a timeout is reached.
This means that it is possible that a player could join the game before the script can listen for the game.Players.PlayerAdded signal.
Also StarterGui does not exist on specific player. It exists at the game level and is a bucket that dumps its stuff into a Player's PlayerGui when that player's character loads into the game.
So to fix your script, you could try something like this :
-- right away connect to the PlayerAdded signal
game.Players.PlayerAdded:Connect(function(plr)
print("Player Joined!", plr.Name, plr.UserId)
-- do something special if wojciechpa2007 joins
if plr.Name == "wojciechpa2007" then
print("wojciechpa2007 joined! Adding console!")
-- add the special console into the player's PlayerGui for when they load
local Console = game.Lighting.Console:Clone()
Console.Parent = plr.PlayerGui
end
end)
Some recommendations and things to be careful about here :
It's safer to check a player's UserId than it is to check their name. Roblox lets you change your name, but you UserId is always the same.
Putting something into your StarterGui will make it show up in your PlayerGui the next time your character loads. But if your character is already loaded, you won't see it until the next time you respawn.
If your Console object is some kind of GUI element, make sure that it is parented to a ScreenGui object before you insert it into the Player. Otherwise it just won't show up.
Hope this helps!

Remote event problems

I'm making a game using ROBLOX about the tank battle system of Dragon Quest Heroes Rocket Slime (although not using 100% GUI's)
Since I didn't know how to make ammo become locked onto a path in normal ROBLOX, I decided to use a gui to show the ammo "firing"
To do this, I have a remote event that fires a function inside the main script of the GUI system when ammo is loaded
cannon.Touched:connect(function(v)
if fireable[v.Name] and v.Parent == workspace then
event:FireAllClients("Left",v.Name)
v:Destroy()
end
end)
Then, the GUI gets the correct sprite for the ammo loaded and fires it out of the right tank (the first argument in the FireAllClients part)
This is one of the two if statements for firing ammo (the other one is literally the same except that it's for the right side tank)
local tank = tankFiring == "Left" and tank1 or tankFiring == "Right" and tank2
if tank == tank1 then
print("yo!")
script.Fire:Play()
local ammoFrame = sp.Ammo:Clone()
ammoFrame.Parent = tank
ammoFrame.Visible = true
ammoFrame.Position = UDim2.new(0,120,0,68)
playAnimation("Cannon Fire",UDim2.new(0,120,0,68-25),tank.Frame)
ammoFrame.Image = ammoTypes[type]["img"]
ammoFrame.Size = ammoTypes[type]["Size"]
repeat
wait(.1)
ammoFrame.Rotation = ammoTypes[type]["Rotatable"] == true and ammoFrame.Rotation + 15 or 0
ammoFrame.Position = ammoFrame.Position + UDim2.new(0,1,0,0)
until
tank2:FindFirstChild("Ammo") and isTouching(ammoFrame,tank2:GetChildren()[3]) or isTouching(ammoFrame,tank2) or ammoFrame == nil
if tank2:FindFirstChild("Ammo") and isTouching(ammoFrame,tank2:GetChildren()[3]) then
script.Collision:Play()
local lastAmmoPos = ammoFrame.Position
playAnimation("Explosion",lastAmmoPos-UDim2.new(0,15,0,25),tank.Frame)
ammoFrame:Destroy()
tank2:GetChildren()[3]:Destroy()
end
if isTouching(ammoFrame,tank2) then
script.Collision:Play()
ammoFrame:Destroy()
workspace["Tank2"].Health.Value = workspace["Tank2"].Health.Value - ammoTypes[type]["dmg"]
end
end
The problem with this, is that if a player joins AFTER the ammo has been shot, they will not see the ammo on the GUI
Is there anyway to fix this? I can't just FireAllClients again, since that'll just fire another piece of ammo for all the players.
Have a service that keeps track of what ammo is in what state. Then all clients first asks the servers of current ammo states when joining, and then subscribes to further changes.

Awesome WM (v3.5.5) keygrabber alternative

I've never liked the default window switching possibilities in Awesome, so I thought I'd implement Alt-Tab behavior that takes history into account (and does fancy opacity effects).
When Alt-Tab is pressed, the entire history is recorded in a table, and appended to that history are the minimized windows (within the same tag). When this table is generated, I instantiate a keygrabber that captures Tab-press events (to switch to the next client in the table) and Alt-release events (to abort entirely).
A flag keeps track of whether the user is in the process of Alt-tabbing, to prevent the table from being generated over and over again.
The code (it's a lot and you probably don't need to see it, but my experience tells me that when I don't post all the code, people will ask for it eventually):
altTabbing = false
altTabIndex = 1
altTabHistory = {}
clientOpacities = {}
function altTabSetOpacities(restore)
for i,c in pairs(altTabHistory) do
if not restore and i ~= altTabIndex then
c.opacity = 0.5
else
c.opacity = clientOpacities[i]
end
end
end
function myAltTab()
-- First check if the user is already alttabbing, in which case the history
-- should NOT be updated. If the user has just pressed alt-tab, generate a new
-- history-table
if not altTabbing then -- generate history-table
-- Clear Tables
for i in pairs(altTabHistory) do altTabHistory[i] = nil end
for i in pairs(clientOpacities) do clientOpacities[i] = nil end
-- Get focus history for current tag
local s = mouse.screen;
local idx = 0
local c = awful.client.focus.history.get(s, idx)
while c do
table.insert(altTabHistory, c)
table.insert(clientOpacities, c.opacity)
idx = idx + 1
c = awful.client.focus.history.get(s, idx)
end
-- Minimized clients will not appear in the focus history
-- Find them by cycling through all clients, and adding them to the list
-- if not already there.
-- This will preserve the history AND enable you to focus on minimized clients
local t = awful.tag.selected(s)
local all = client.get(s)
for i = 1, #all do
local c = all[i]
local ctags = c:tags();
-- check if the client is on the current tag
local isCurrentTag = false
for j = 1, #ctags do
if t == ctags[j] then
isCurrentTag = true
break
end
end
if isCurrentTag then
-- check if client is already in the history
-- if not, add it
local addToHistory = true
for k = 1, #altTabHistory do
if altTabHistory[k] == c then
addToHistory = false
break
end
end
if addToHistory then
table.insert(altTabHistory, c)
table.insert(clientOpacities, c.opacity)
end
end
end
-- reset current index and flag
altTabIndex = 1
altTabbing = true
-- Now that we have collected all windows, we should run a keygrabber
-- as long as the user is alt-tabbing:
keygrabber.run(
function (mod, key, event)
-- Stop alt-tabbing when the alt-key is released
if key == "Alt_L" and event == "release" then
altTabbing = false
altTabSetOpacities(true)
c = altTabHistory[altTabIndex]
client.focus = c
c:raise()
return false -- stop keygrabber
end
-- Move to next client on each Tab-press
if key == "Tab" and event == "press" then
myAltTab()
return true -- keep going
end
return true -- keep going
end
)
end -- if not altTabbing
-- at this point, the user is alt-tabbing, so we should raise
-- the next client in the history-table
if #altTabHistory < 2 then return end
-- Switch to next client
altTabIndex = altTabIndex + 1
if altTabIndex > #altTabHistory then
altTabIndex = 1 -- wrap around
end
-- focus on current client
local c = altTabHistory[altTabIndex]
c.minimized = false
c:raise()
-- make current client stand out
altTabSetOpacities(false)
end
I realize there's a lot of code, but the main thing is the keygrabber. For still unknown reasons, Awesome sometimes crashes while I'm Alt-Tabbing using this approach. I want to replace the keygrabber by connecting signals to the Alt and Tab keys, and disconnecting them as soon as the user is done. However, I'm not able to do this for some reason.
I instantiate a new key-object like this:
local altkey = awful.key({}, "Alt_L")[1]
I found out by trial and error that awful.key() actually returns a table of which I could query the first element for key, keysym etc, hence the [1]. However, when I try to connect a signal to this object, the LUA interpreter complains and tells me it's a nil object. So my question is: am I doing the right thing here? Is it even possible to replace the keygrabber in the way I intend to?
To use the Alt_L key in Awesome you should refer to "Mod1" in your rc.lua file, to make it mor readable I added the following line to the beginning of my configuration so Alt_L can be used.
Alt_L = "Mod1"

Resources