I want to make it so that the music for my game waits a few seconds before playing. I tried my best off of what I knew to make a script that does so. I put my script under a part. The part is anchored and can collide is turned off. The player spawns right in the part. I did this because I only know how to do this to activate stuff:
local part = script.Parent
part.Touched:Connect(NeedSomethingToPutHere)
I tried my script and it didn’t work. I made it so that once the player touches the part the function can’t be activated again by not changing the waitTime to false again. This has worked for me in the past. I did my best off of what I know because I don’t know a lot about how to make scripts.
This is the script I used:
local music = game.SoundService.SoundGroup.Sound
local part = script.Parent
local waitTime = false
local function Audio()
if waitTime == false then
waitTime = true
music.Playing = false
wait(10)
music.Playing = true
end
end
part.Touched:Connect(Audio)
I messed around with stuff a bit but nothing worked. I looked all over the place but I couldn’t find any answers. I’m not sure if I made a minor mistake or if the whole thing is completely wrong. EDIT:
I don’t want to rewrite this entire thing so I’m putting this here. I moved the sound to workspace and out of the sound group. The script works fine except for one problem. I have it so the player spawns into a part that teleports them after 10 seconds. The audio also waits 10 seconds before playing. What’s the problem then? The two events aren’t synced up with each other which I could try and modify the wait time to make them synced up but I have a feeling it’s related to load time so that wouldn’t work. It might work if I make it wait until the player is loaded in and wait 10 seconds however I’m not sure how to do that.
Here is a the script and video though:
local music = game.Workspace.Sound -- the sound
local waitTime = 10 -- change it to your likings
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
if char then -- if the character has loaded
task.wait(waitTime) -- waits
music:Play() --plays music
else
print("Character not loaded")
end
end)
end)
local music = game.SoundService.SoundGroup.Sound -- the sound
local waitTime = 10 -- change it to your likings
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
if char then -- if the character has loaded
task.wait(waitTime) -- waits
music:Play() --plays music
else
print("Character not loaded")
end
end)
end)
@re_CAPTCHAboy and @whereiswayne the scripts don’t do anything. Nothing shows up in output either except stuff that happens every time. The only errors that appear are Requested module experienced an error while loading - Client and Unable to load rbxasset://avatar/characterR15.rbxm - Client. These two errors show up twice first time for server second time for client. I don’t know if this could cause any future issues or is already causing some with scripts.
I think the problem has to do with the sound being parented under SoundService, when I parented it under Workspace the script worked fine.
Additionally, if the script is supposed to activate a single time when a player joins, why does it need to be under a touched function? Can’t you just leave the script without it, or am I missing something?
local music = game.SoundService.SoundGroup.Sound
wait(10)
music.Playing = true
I might make it a bit more complicated here, but try this:
local music = game.SoundService.SoundGroup.Sound -- the sound
local waitTime = 10 -- change it to your likings
local function exists(item) if item then return true else return false end end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
if exists(char) then -- if the character has loaded
task.wait(waitTime) -- waits
music:Play() --plays music
else
repeat task.wait() until exists(char)
if exists(char) then
task.wait(waitTime)
music:Play()
end
end
end)
end)
I tried this is workspace and it worked! There is a slight issue though… I have it so when the player spawns it touches a part that waits 10 seconds and then teleports them. I also have it so that the wait time for the audio to play is 10 seconds. Even though it’s the same amount of wait time the music plays my guess is around a second before the player gets teleported.
This is what it actually looks like (test set up) [First recording it was synced up for whatever reason so this is the second recording where it actually happened.] robloxapp-20230115-1804275.wmv (294.4 KB) (Yes it’s a 2D game and yes I have been working on it for months with little skill and yes I have been working on this 99.9% by myself without the help with my friend who I work on games together with so I decided to make it a mostly solo project with help that I get from others like here.)
This only worked in the first attempt. After that it didn’t work. I am going to test this out on the actual game instead of inside studio because I noticed Roblox acts differently between the two. (Example: in Roblox studio I have text that appears in a certain font but while playing the actual game the font doesn’t load correctly) Edit: it doesn’t work in studio or in the actual game.
wait() is the most common mistake in begineers because it was used majoritaly from Roblox’s creation till recently. (Which means most tutorials use wait() over task.wait())
wait() is now deprecated and roblox made a new system (Of simmilar wait functions) called task. The correct version of wait() has been changed to task.wait() as wait() is no longer supported
The difference? task.wait() has 2X better ticking method (I think thats what you call it) which means it does its job better by 2X (atleast to what the docs taught me).
Another reason is that unlike wait(), task.wait() does not throttle and guarantees the resumption of the thread on the first Heartbeat that occurs when it is due.
Basiaclly its the better version of wait() and should not cause that error. It is not task.wait() switching that causes that error.
local music = game.Workspace.Sound -- the sound
local waitTime = 10 -- change it to your likings
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
if char then -- if the character has loaded
task.wait(waitTime) -- waits
music:Play() --plays music
else
print("Character not loaded")
end
end)
end)
This works for me… Is this the solution and I’m just not understanding that this is the fix you found? What is the issue here. It works? (Script is in ServerScriptServer & Sound is in Workspace). This is an issue caused by an external script in that case.
I tried that and it still didn’t work, thanks for the tip though because it allowed me to make a part move faster and smoother which I’ve been trying to do. I think it may be because the part can’t detect the player right away which causes the delay. I would need a teleport script that isn’t touch related but I only know how to do touch ones. Either that or I make CanTouch false for 10 seconds then true and get rid of the wait time inside of the script so that way it should detect the player right away. Edit: If I do that the player teleports BEFORE the music plays. I think that if I were to use a script that is similar to the script for waiting to play music in the fact of how it waits for the character to load it might sync up then. Also when I look in output it never says “Character not loaded” at all.
Because the character always loads. As long as your sound is called Sound with capital the this works try to rename your sound and check it works (also out script in server script service if not already).
If it doesn’t work then it is an external issue caused by another one of your scripts
That is what I’m looking for. If that doesn’t work I’m out of ideas. All the scripts I used are essential and I don’t know how to change them to make them still do the same process. I hope this won’t be like the “player movement solution loop” where I couldn’t find a solution anywhere to how to make it so the player can’t move forward and backward because that was a nightmare. I lasted a while so I hope the same thing doesn’t happen with this. I hope I can find a solution that works because if not then it will be difficult with what I’m doing. (cut scenes made from moving the player around and camera angle with transparent parts the the camera can go through.)
local music = game.Workspace.Sound -- the sound
local waitTime = 10 -- change it to your likings
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
if char then -- if the character has loaded
task.wait(waitTime) -- waits
music:Play() --plays music
else
print("Character not loaded")
end
end)
end)