Hello there, I tried to call a function using an if statement instead of the commonly used method, but it isn't working, how do I fix this? This is the code (Note that this is in LocalScript):
local ReplicatedStorage = game:GetService("ReplicatedStorage") local CaptureEvent = ReplicatedStorage:WaitForChild("CaptureEvent") local TeleportPart = game.Workspace.TeleportPart local plr = game:GetService("Players").LocalPlayer local function trigger() plr.PlayerGui.AllyGui.Button.LocalScript.Disabled = false plr.PlayerGui.AllyGui.TeamOnlyGuiScript.Disabled = false plr.PlayerGui.CentralGui.Enabled = false CaptureEvent:FireServer(TeleportPart) end if TeleportPart.BrickColor == BrickColor.new("Maroon") then trigger() end
-NOTE- I've updated the answer please recheck the code...
I believe the problem isn't the function in the if-statement but the if-statement itself. Without some other fancy code, Lua runs in order, this means that your script runs in order, declares a function then checks if the BrickColor
is Maroon. If statements aren't event listeners so it only checks once and if it's false the second it's checked then it doesn't do anything. Assuming that the TeleportPart
's BrickColor
changes after the script runs you can instead add an events listener like so:
TeleportPart:GetPropertyChangedSignal("BrickColor"):Connect(trigger)
This would replace the if-statement.
I hope this helped! If this fixed the problem please mark it as the answer.