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

Script is not updating the wave value. How do I fix it?

Asked by 5 years ago
Edited 5 years ago

INFO: The script won't update "Wave Value" in Workspace. The script is in ServerScriptService. The time is shortened for testing it should be longer when finished same with the for loop.

local ServerStorage = game.ServerStorage
local wave = workspace.Wave.Value

function CreateWave()
    local WaveClone = ServerStorage.Wave:Clone()
    WaveClone.Parent = game.Workspace
end

wait(1)
while true do
    for i = 1, 1 do
        if math.random(1, 5) == 2 then
            local EClone = ServerStorage.EWave:Clone()
            EClone.Parent = game.Workspace
        else
            if math.random(1, 10) == 2 then
                local SnowClone = ServerStorage.GiantSnow:Clone()
                SnowClone.Parent = game.Workspace
            else
                CreateWave()
            end
        end
        wait(math.random(5, 15))
    end
    wave = wave + 1
    wait(1)
end

1 answer

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

The problem lies in the fact that you wrong assume that the wave variable is a reference to the actual property of the IntValue. If the value of the IntValue changes, the variables you have defined won't update; only its Value property will change. In order to fix this, you can remove the wave variable and only make direct accesses to the Value property of the IntValue. You could also modify the variables to hold the IntValue object itself.

local ServerStorage = game:GetService("ServerStorage")
local wave = game.Workspace.Wave -- refer to object! 

function CreateWave()
    local WaveClone = ServerStorage.Wave:Clone()
    WaveClone.Parent = game.Workspace
end

wait(1)
while true do
    for i = 1, 1 do
        if math.random(1, 5) == 2 then
            local EClone = ServerStorage.EWave:Clone()
            EClone.Parent = game.Workspace
        else
            if math.random(1, 10) == 2 then
                local SnowClone = ServerStorage.GiantSnow:Clone()
                SchanowClone.Parent = game.Workspace
            else
                CreateWave()
            end
        end
        wait(math.random(5, 15))
    end
    wave.Value = wave.Value + 1 -- directly access property 
    wait(1)
end

It is important to note the difference between a reference and a value. Think of a reference as holding an actual memory location whilst a value just holds normal information (for example, a boolean). In Lua, for example, tables are passed by reference.

local t1 = {
    foo = "abc", 
    bar = 123, 
    foobar = true
}

local t2 = {
    foo = "abc", 
    bar = 123, 
    foobar = true
}

local t1reference = t1
--t1 & t1reference are the same table now, but t2 is different 

t1.foo = "Hello world!"
print(t1.foo) --> Hello world!
print(t1reference.foo) --> Hello world!

print(t2.foo) --> abc

The t1 and t1reference variables both hold the same exact table whilst t2 is a different table even though it holds the same values.

However, data like booleans and numbers are passed via value.

local number = 50 
local numberVal = num

number = 25
print(number) --> 25
print(numberValue) --> 50

local bool1 = false
local bool1Val = bool1
bool1 = true
print(bool1) --> true
print(bool1Val) --> false 

Even though numberValue was set to number, both variables have different memory and so changing one value does not change the other value. Same for the boolean values.

Ad

Answer this question