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

How can I make a function fire depending on a value from a script in Starter Pack?

Asked by 5 years ago
Edited 5 years ago

I have a script in StarterPack that only becomes enabled if a value equals something.

enter code here
local playerCharacter = script.Parent.Parent.Character

local characterShirt = playerCharacter:WaitForChild("Shirt")

local characterPants = playerCharacter:WaitForChild("Pants")





--Make Characters







function AlyxCharacter()

playerCharacter.Head.BrickColor = BrickColor.new("Light orange")

playerCharacter["Left Arm"].BrickColor = BrickColor.new("Light orange")

playerCharacter["Left Leg"].BrickColor = BrickColor.new("Light orange")

playerCharacter["Right Arm"].BrickColor = BrickColor.new("Light orange")

playerCharacter["Right Leg"].BrickColor = BrickColor.new("Light orange")

playerCharacter.Torso.BrickColor = BrickColor.new("Light orange")

characterPants.PantsTemplate = "rbxassetid://2605757306"

characterShirt.ShirtTemplate = " "

local hair2 = script.Hair2

hair2:Clone().Parent = playerCharacter

end



if game.ReplicatedStorage.StatusTag.Value == "Match" then

AlyxCharacter()

print("CharacterChanged")

end

The issue I have with this is that when the value equals "Match" the function does not fire. The weird thing is that if the value equals "Match" before I play test the game, it works.

I have no idea to fix this and am totally lost.

1 answer

Log in to vote
0
Answered by 5 years ago

The reason why it runs normally when you run the game when "Match" is the value and not when it's changed is because the script runs once when you start the game, and instructions were not given to check it again, or detect a change in the value.

if game.ReplicatedStorage.StatusTag.Value == "Match" then

AlyxCharacter()

print("CharacterChanged")

end

So, when the place runs, it checks the value, and never checks it again.

You can fix it in 2 ways:

You can use the .Changed event, which fires when any property in the value is changed.

game.ReplicatedStorage.StatusTag.Changed:Connect(function()
    if game.ReplicatedStorage.StatusTag.Value == "Match" then
        AlyxCharacter()
        print("CharacterChanged")
     end
end)

Or you can use :GetPropertyChangedSignal(), which only checks if a specific property in the value is changed, which is the Value property in this case.

game.ReplicatedStorage.StatusTag:GetPropertyChangedSignal("Value"):Connect(function()
    if game.ReplicatedStorage.StatusTag.Value == "Match" then
        AlyxCharacter()
        print("CharacterChanged")
    end
end

I hope this helped!

0
Or use a repeat wait() until loop. DeceptiveCaster 3761 — 5y
0
^ Yeah, you can do that as well. BladeStar37 0 — 5y
Ad

Answer this question