Local function not running when called?

Hiya Developers!

Please excuse me if this is a really simple question, i have a local script placed inside of ServerScriptService with a function inside it.

local function BeforeRoundSetUp()
	print("Test")
	
		end
BeforeRoundSetUp()

For some reason, when the function is called at the end, nothing happens? Nothing gets printed into the console.

Thanks!

1 Like

only ServerScripts work in ServerScriptService.
Localscripts only work in:

1 Like

Thanks so much, i thought that would be the issue so i just threw it somewhere else but that didn’t work haha!

Thanks for the list, appreciate it!

Hm, i just tested the script, and it doesn’t seem to work when placed in any of these locations? Nothing is printed when the function is called?

Maybe you are just in the server side, this happen because you only run the game(and not pressing play) and so no player, so no local stuff, so no local script

Nope, i have pressed the play button and my character has fully loaded inside of the game.

Make sure that the Script you’re using is a full on Server Script, and that no other scripts are possibly preventing it from firing

And also try adding a print before you even begin to call the function, something like this should work:

local function BeforeRoundSetUp()
    print("Function ran")
end

print("Script should be working fine!")
BeforeRoundSetUp()

If the “Script should be working fine!” print does not Output back, then there’s something preventing it from firing

Is there something stopping the function from being called, ei another function or loop running so it cannot run?

Hm yeah, that hasn’t fired. I do have another script, which just detects when a button is pressed and sets a value.

local button = player.PlayerGui["MainMenu UI"].BackgroundFrame.TeamButtonFOUR
local player = game:GetService("Players").LocalPlayer

function buttonPressed ()
	game.ReplicatedStorage["Team Value"]["Team Number"].Value = 4
	print("4")
	
	
	
end

button.MouseButton1Click:Connect(buttonPressed)

What I would suggest is:

  • Try to remove/uninstall all Plugins within your Studio, maybe you installed something malicious?

  • Check for any malicious stuff you may have added within your game if possible

  • Create a new place on an empty Baseplate, and try the same scenario that you were trying to accomplish

  • Make sure that the Script Properties (Both Enabled = true, and Disabled = false) are set to the correct settings
    image

  • Search for any scripts that may be interfering with the BeforeRoundSetUp() function

image

If Option 3 doesn’t work, then I’d suggest reinstalling your Studio to make sure that it’s been fully refreshed

Thanks for the suggestions, i’ll try that soon!

Here is my whole script if this helps, it’s just a personal game i am making for me and friends haha.
Ignore if my coding method is inefficient, i am learning lol.

local player = game:GetService("Players").LocalPlayer
local plrJoined = player.PlayerAdded

-- Short Cuts
local mainMenuEnabled = player.PlayerGui["MainMenu UI"].Enabled
local roundConfigEnabled = player.PlayerGui["Round Config"].Enabled
local teamAmount = game.ReplicatedStorage["Team Value"]["Team Number"].Value

-- True / False Variables
local hasCompletedTeamPhase = false

-- Main Function Body
local function BeforeRoundSetUp()
	print("hi")
	while true do -- Loop until a team has been selected
		task.wait(1) -- Stop game crashing on while loop
		if teamAmount == 1 then
			hasCompletedTeamPhase = true
			mainMenuEnabled = false
			roundConfigEnabled = true
		elseif teamAmount == 2 then
			hasCompletedTeamPhase = true
			mainMenuEnabled = false
			roundConfigEnabled = true
		elseif teamAmount == 3 then	
			hasCompletedTeamPhase = true
			mainMenuEnabled = false
			roundConfigEnabled = true
		elseif teamAmount == 4 then
			hasCompletedTeamPhase = true
			mainMenuEnabled = false
			roundConfigEnabled = true
			
			
			if hasCompletedTeamPhase == true then -- as soon as a team has been selected stop the loop
				break -- end loop
			end
	end
	end
end
print("Script should be working fine!")
BeforeRoundSetUp()

This could be why here, PlayerAdded is an Event provided by the Players Service, not the individual LocalPlayer as they’re 2 different things here

If you’d like to check when a Player has been added to the game, use the game.Players.PlayerAdded Event instead for that

Another potential error you might get, is that the PlayerGui may not properly load on time the moment the LocalPlayer gets added, so you’d want to call the WaitForChild() function to ensure that object is in there

Tidying up your script a bit, try this and see if it works or not:

local Plrs = game:GetService("Players")
local RepS = game:GetService("ReplicatedStorage")

local Plr = Plrs.LocalPlayer
local PlrGui = Plr:WaitForChild("PlayerGui")

-- Short Cuts
local mainMenuEnabled = PlrGui:WaitForChild("MainMenu UI").Enabled
local roundConfigEnabled = PlrGui:WaitForChild("Round Config").Enabled
local teamAmount = RepS:WaitForChild("Team Value"):WaitForChild("Team Number").Value

-- True / False Variables
local hasCompletedTeamPhase = false

-- Main Function Body
local function BeforeRoundSetUp()
	print("hi")
	while true do -- Loop until a team has been selected
		task.wait(1) -- Stop game crashing on while loop
		if teamAmount == 1 then
			hasCompletedTeamPhase = true
			mainMenuEnabled = false
			roundConfigEnabled = true
		elseif teamAmount == 2 then
			hasCompletedTeamPhase = true
			mainMenuEnabled = false
			roundConfigEnabled = true
		elseif teamAmount == 3 then	
			hasCompletedTeamPhase = true
			mainMenuEnabled = false
			roundConfigEnabled = true
		elseif teamAmount == 4 then
			hasCompletedTeamPhase = true
			mainMenuEnabled = false
			roundConfigEnabled = true


			if hasCompletedTeamPhase == true then -- as soon as a team has been selected stop the loop
				break -- end loop
			end
		end
	end
end

print("Script should be working fine!")
BeforeRoundSetUp()
1 Like

Thanks for all the detailed tips, this will be really helpful!

The script does in-fact work, the function ran and also the little print at the bottom ran. The only issue i am facing now is the first UI does not disappear when the buttons are clicked.

I tried to simply just disable the MainMenu and enable to Config UI but that does’t seem to work. Maybe it’s just another issue i have made?

I added a print after if teamAmount == 1 then and nothing gets printed there, so i assume i have messed something up here?

This is because you’re saving a Value within your teamAmount variable and the same goes for your mainMenuEnabled & roundConfigEnabled variables, the reason being is because when you reference a property of an Instance using a variable, it gets saved until you re-change the variable to something else

Personally for me, instead of using a Loop I would use something called a Changed Event which are exclusively for only Value Instance

These things fire whenever a value of that said “Instance” changes, and it can help tidy out our script to make it look a bit neater & slightly reduce performance as well:

local Plrs = game:GetService("Players")
local RepS = game:GetService("ReplicatedStorage")

local Plr = Plrs.LocalPlayer
local PlrGui = Plr:WaitForChild("PlayerGui")

-- Short Cuts
local mainMenu = PlrGui:WaitForChild("MainMenu UI")
local roundConfig = PlrGui:WaitForChild("Round Config")
local teamAmount = RepS:WaitForChild("Team Value"):WaitForChild("Team Number")

-- True / False Variables
local hasCompletedTeamPhase = false

-- Main Function Body

local function CheckTeam(NewValue)
    if NewValue == 1 or NewValue == 2 or NewValue == 3 or NewValue == 4 then
		hasCompletedTeamPhase = true
		mainMenu.Enabled = false
		roundConfig.Enabled = true
	end
end

CheckTeam(teamAmount.Value) -- In case you want to fire it the moment you join, you can use this

teamAmount.Changed:Connect(CheckTeam) -- This will change the MOMENT a Player's Team changes and will check if it's 1-4,

I modified this script, so hopefully everything should work out just fine and if it doesn’t feel free to debug it with print() statements

1 Like

Ayy, thanks so much for your help this evening!
I’ll be sure to learn of the tips you’ve taught me and use them in my future development!

Thankfully, all is working. Thanks for the more efficient script also!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.