Can't change value of IntValue by using RemoteEvent

This is the repost, here is the original.

I’m try to learn about RemoteEvent, so i’m start by write this piece of code:

  • Local script:
    local remoteEvent = game.ReplicatedStorage.RemoteEvent
    local random = math.random(1, 10)

    remoteEvent:FireServer(random)

  • Server script:
    local remoteEvent = game.ReplicatedStorage.RemoteEvent
    local intValue = workspace.IntValue.Value --intValue = 0

    local function changeValue(player, value)
    print(value)
    intValue = value
    print(intValue)
    end

The weird thing is the output print the changed value if IntValue, but when I check the properties window, it still 0. :frowning_face:

I think the issue with your code is this:

You have one IntValue Variable on the script, and one IntValue object (a physical element of your game). What your code essentially does, is that it changes the IntValue Variable to the number of the value, rather than the Value of that IntValue.

To fix this, you can change this part of your server code, and it should work.

local function changeValue(player, value)
print(value)
intValue.Value = value -- instead of intValue on its own.
print(intValue.Value) -- to reference the value of the IntValue, do intValue.Value
end

This should solve your predicament.

1 Like

Wait, I mistype, again… This has been happen to my original post :expressionless:.

That’s okay! Feel free to correct your code and I’ll help out.

1 Like

No I mean, I really change the code but that not really do anything.

Your script above would still not work, because you are gathering the value from the IntValue. Try out this set of code:

local remoteEvent = game.ReplicatedStorage.RemoteEvent
local intValue = workspace.IntValue

local function changeValue(player, value)
print(value)
intValue.Value = value -- you need to reference the .Value here, not in the variable.
print(intValue.Value)
end

If you do intValue.Value, it’ll return the number stored inside, but it’ll disregard any association to the original variable. As such, you need to call the .value inside of the algorithm.

1 Like

Oh I get it, thank you so much.

1 Like