Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to call a function using if statement?

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

1 answer

-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.

2 comments

0

BrickColor.Changed does not exist. You're looking for Instance:GetPropertyChangedSignal

0

I never knew that thanks, I'll edit the answer now.

This discussion is preserved as it was. New answers, comments, and votes are closed.