Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to call a function using if statement?

Asked by
R0jym 47
2 years ago

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

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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

0
BrickColor.Changed does not exist. You're looking for Instance:GetPropertyChangedSignal Ziffixture 6913 — 2y
0
I never knew that thanks, I'll edit the answer now. strangejmaster43 139 — 2y
Ad

Answer this question