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

Script dosent detect value change even with no errors in output and everything being set up?

Asked by 4 years ago

CODE:

local AI = script.Parent.Parent.Timer.PositionNumber

local Lighting = game.ServerStorage

local World = game.Workspace

local Footstep = script.Parent.Parent.Timer.RemoteFunction.Footstep.Sound


--1--
while AI.Value == 1 do
    wait() 
        script.Parent.Transparency = 1
print("worked")
    Lighting.Walk1:Clone().Parent = game.Workspace
    World.Neutral:Destroy()
    Footstep:Play()
    break
end
--1--

The problem is that there are no errors in output, but the script isnt doing the following code inside it. The loop is needed the detect the change in the value, and the break is needed so it dosent clone and destroy forever, and can move on to whatever is on the next line of code.

However, despite no errors in output and everything in the scirpt being set up right, it just dosent work. Any help?

0
If you're changing the value localy aka : LocalScript then that's why. VitroxVox 884 — 4y
0
its a serverscript Adenandpuppy 87 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

From my assumption, I'm assuming you wish to detect when AI.Value == 1 so you can run the code block inside.

Now within the code you provided, I'm seeing that you have it set up so that while the AI value is equal to one, it will run the code. The issue with this is the fact that it will continuously repeat the line of code only when the AI value is 1. This is an issue, since if the while AI.Value == 1 is ran prior to the value being one, the loop will never be initialized, making it so that the code never runs, even when the value actually does equal 1.

The first solution to this is by creating an event which triggers when the value changes, such as through bindable events, or even module scripts if you truly want to get creative with it. This is considered the most optimal solution.

The second, simpler solution is to do the following:

repeat game:GetService("RunService").Stepped:Wait() until AI.Value == 1

--Run Code--

script.Parent.Transparency = 1
print("worked")
Lighting.Walk1:Clone().Parent = game.Workspace
World.Neutral:Destroy()
Footstep:Play()

This essentially repeatedly waits until the AI.Value is equal to 1, so you can then run the code exactly once, this removes the need for a break, as well as the waits. I'd say this is the most optimal way to do the following without the usage of any events, thought correct me if I'm wrong on this one.

If I messed up on anything, please provide corrections and or suggestions on what to add in the comments.

Ad

Answer this question