Functions not working

Basically, when the player character gets added, its supposed to play another function and a sound
but neither of them are playing

local function MenuON()
	MainMenu.Enabled = true
end
player.CharacterAdded:Connect(function()
	MenuON()
	LobbyMusic:Play()
end)

Could you specify more please?

2 Likes

Any errors? If not, have you tried it while resetting? Once again, if not, that line of code might be registering after the character has already loaded.

2 Likes

What exactly?

  • Local Script
  • Tried using the :Play() outside of the functions and it worked fine
2 Likes

Try this:

local function MenuON()
	MainMenu.Enabled = true
end

local function enableOnLobby()
	MenuON()
	LobbyMusic:Play()
end)

player.CharacterAdded:Connect(enableOnLobby)
task.spawn(enableOnLobby)
1 Like

if its a localscript check if its placed in starterpack, startergui, starterplayerscripts, startercharacterscripts, or replicatedfirst

If its not in one of those localscripts cant work

1 Like

Didn’t work, it gave me an error

And yes its in Starter GUI

1 Like

I mean, we cannot help you out without much information, maybe could you proportionate more of the script?

1 Like

Its saying that β€œenableOnLobby” is not a function… strange

1 Like

Well there isn’t much in the script except alot above it except variables for other parts of the script which work fine

local wfc				=game.WaitForChild
local ffc				=game.FindFirstChild
local player			=game.Players.LocalPlayer
local Char				=player.Character
local playerteam		=player.Team.Name
repeat wait(.1) until game:IsLoaded()
--SERVICES
local SoundService			=game:GetService("SoundService")
local TweenService			=game:GetService("TweenService")
local lighting 				=game:GetService("Lighting")
local StarterGui			=game:GetService("StarterGui")
local ReplicatedStorage		=game:GetService("ReplicatedStorage")
local ServerStorage			=game:GetService("ServerStorage")
local Teams					=game:GetService("Teams")
local TCS 					=game:GetService("TextChatService")
local TweenService 			=game:GetService("TweenService")
local GunsWS				=wfc(ReplicatedStorage,"GunsWS")
local SpawnWeaponRE			=wfc(ReplicatedStorage,"SpawnWeaponRE")
--ScreenGUIS
local PlayerGui				=wfc(player,"PlayerGui")
local MainMenu				=wfc(PlayerGui,"MainMenu")
local LoadoutGui			=wfc(PlayerGui,"LoadoutGui")
local DevLogScreen			=wfc(PlayerGui,"DevLogScreen")
local SettingsScreen		=wfc(PlayerGui,"SettingsScreen")
--Buttons
local PlayButtonFrame		=wfc(MainMenu,"PlayButtonFrame")
local LoadoutButtonFrame	=wfc(MainMenu,"LoadoutButtonFrame")
local TeamChangeButtonFrame	=wfc(MainMenu,"TeamChangeButtonFrame")
local SettingsButtonFrame	=wfc(MainMenu,"SettingsButtonFrame")

local PlayButton			=wfc(PlayButtonFrame,"PlayButton")
local ToggleLoadoutGui		=wfc(LoadoutButtonFrame,"ToggleLoadoutGui")
local TeamChangeButton		=wfc(TeamChangeButtonFrame,"TeamChangeButton")
local SettingsButton		=wfc(SettingsButtonFrame,"SettingsButton")

local ButtonGradient		={
	wfc(PlayButtonFrame,"UIGradient"),
	wfc(PlayButton,"UIGradient"),
	wfc(LoadoutButtonFrame,"UIGradient"),
	wfc(ToggleLoadoutGui,"UIGradient"),
	wfc(TeamChangeButtonFrame,"UIGradient"),
	wfc(TeamChangeButton,"UIGradient"),
	wfc(SettingsButtonFrame,"UIGradient"),
	wfc(SettingsButton,"UIGradient"),}
local ButtonFrames			={
	PlayButtonFrame,
	LoadoutButtonFrame,
	TeamChangeButtonFrame,
	SettingsButtonFrame,}

local GameTitle 			=wfc(MainMenu,"GameTitle")
--TEAMS
local ChangeTeam			=wfc(ReplicatedStorage,"ChangeTeam")
local TeamCLS				=wfc(GunsWS,playerteam).Value
local CatTeamCLS			=wfc(GunsWS,"Cat union").Value
local DogTeamCLS			=wfc(GunsWS,"Dog union").Value
local Catunion 				=ffc(Teams,"Cat union").name
local Dogunion 				=ffc(Teams,"Dog union").name
local Spectators 			=ffc(Teams,"Spectators").name
--REST
local ClickSound			=wfc(SoundService,"ClickSound")
local ErrorSound			=wfc(SoundService,"XError")
local LobbyMusic			=wfc(SoundService,"LobbyMusic")
--SPECIFIC FOR THIS SCRIPT
local loadout 				=wfc(LoadoutGui,"Loadout")
local LobbyFolder			=game.Workspace.Lobby
--━━━━━━━━━━━━━━━━━━━━━━━━━━━| SETUP |━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━--
TeamChangeButton.Text = "Your Team:\n"..playerteam.."\nClick to Change Team"
--local LMT = TweenService:create(LobbyMusic, TweenInfo.new(10), {Volume = 1})
function MenuON()
	MainMenu.Enabled = true
	LobbyMusic:Play()
end
local function Play()
	if playerteam == Spectators then
		PlayButton.Text		="Please Change Your Team"
		ErrorSound:Play()
		wait(3)
		PlayButton.Text		="Play"
	else
		SpawnWeaponRE:FireServer(player)
		MainMenu:Destroy()
		LobbyMusic.Volume = 0
		ClickSound:Play()
	end
end
PlayButton.MouseButton1Down:Connect(function()
	Play()
end)
--━━━━━━━━━━━━━━━━━━━━━━━━━━━| PLAYER DEAD |━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━--
player.CharacterAdded:Connect(function()
	MenuON()
end)

As the script is in StarterGui, do I put that Script part like in replicated Storage?

Try printing enableOnLobby, it should be a function.
Or use pcall.
Yeah try using pcall

Since when you tried to run the code @12345koip gave you it errored that the first argument to task.spawn is invalid (not a function), and i dont know why that would happen.

If I post the script from above 1:1 it will give me an error that the function isn’t closed right


when I close the functions and print it, it will return β€œnil”

1 Like

Oh shoot i just noticed something, theres an end missing.

1 Like

Oh, so thats the problem. Somehow someway the function is not correctly being declared, which when printing resolves to nil.
Or it may be that the function was set to nil somewhere (MenuOn = nil)

image
I closed the function again and it printed this now,
It did also play, so I guess it is working fine now

With the task.spawn function that @SubLimitless gave you?
If so, then it must have been that it was yielding, so nothing happened (since task.wait runs it in a new thread). Although i dont see anything that could yield, so im not sure.

Thanks for the help! I can now finally go on to the next script

Im asuming task.spawn was the fix because the function wasnt malformed in the original post

Oop sorry i got confused i meant task.spawn