Values changing to amount I did not set it to

My script:

game.ReplicatedStorage.Events.Boosts.Extratime2.OnServerEvent:Connect(function(player)
	if done == false then
		print("Extra Time fired")
		done = true
		player:FindFirstChild("ExtrasBought").Value -= 1
		game.ReplicatedStorage.Minutes.Value += 5
		wait(10)
		done = false
	end
end)

It’s meant to add 5 minutes to the timer, yet it goes from 2 to 57. What could be causing this?

The remote event only fires once

1 Like

The issue could be with how the game.ReplicatedStorage.Minutes.Value is being updated. Based on the code provided, it seems that the initial value of game.ReplicatedStorage.Minutes.Value is 2.

When the event is fired, the value of game.ReplicatedStorage.Minutes.Value is increased by 5 to 7. However, since the code is inside a loop that waits for 10 seconds before setting the done variable back to false , the loop runs 5 times (50 seconds) before the done variable is reset. During this time, any further updates to game.ReplicatedStorage.Minutes.Value will continue to increase the value.

Therefore, if any other events or updates are happening that are increasing the game.ReplicatedStorage.Minutes.Value during this 50 second period, it could cause the value to exceed 60 (which is causing it to wrap around to 0 and continue counting up).

To fix this, you could either ensure that the event only fires once, or update the code to not rely on the done variable to limit the event to only firing once. Additionally, you could consider using a while loop instead of a wait statement to better control the timing of the event.

2 Likes

I have actually added the done loop to try and make it work. This was not part of my original code, but adding the done loop did not fix it back then.

I have added a print statement above, which only prints once, showing it only fired once.

1 Like

If the event is only firing once, the issue is likely caused by another part of your code that is updating the game.ReplicatedStorage.Minutes.Value variable.

Based on the code you provided, it looks like the game.ReplicatedStorage.Minutes.Value variable is being increased by 5 every time the Boosts.Extratime2 event is fired. However, if there is other code running that also updates this variable, it could cause unexpected behavior.

You could try adding print statements to any other code that updates game.ReplicatedStorage.Minutes.Value to see if it’s being updated unintentionally. Additionally, you could consider adding a check to ensure that game.ReplicatedStorage.Minutes.Value never exceeds 60 to prevent it from wrapping around to 0 and counting up. For example:

game.ReplicatedStorage.Events.Boosts.Extratime2.OnServerEvent:Connect(function(player)
    if done == false then
        print("Extra Time fired")
        done = true
        player:FindFirstChild("ExtrasBought").Value -= 1
        game.ReplicatedStorage.Minutes.Value = math.min(game.ReplicatedStorage.Minutes.Value + 5, 60) -- update the minutes value and ensure it doesn't exceed 60
        wait(10)
        done = false
    end
end)

This will ensure that the game.ReplicatedStorage.Minutes.Value variable is never increased beyond 60, preventing it from wrapping around to 0 and counting up.

1 Like

Can we see the script that fires the event?

1 Like
extra.MouseButton1Click:Connect(function()
	if lplr:FindFirstChild("ExtraTimeBought").Value == 0 then
		game.ReplicatedStorage.Events.Boosts.Extratime2:FireServer()
	else
		return
	end
end)
1 Like

I don’t know what causes your scrit problems

local repl = game:GetService("ReplicatedStorage")
repl.Events.Boosts.Extratime2.OnServerEvent:Connect(function(player)
	if done == false then
		print("Extra Time fired")
		done = true
		player:FindFirstChild("ExtrasBought").Value -= 1
		repl.Minutes.Value += 5 --try repl.Minutes = repl.Minutes + 5
		task.wait(10)
		done = false
	end
1 Like

I will try this, I did just discover there is another script causing this, as I set this script to all comments, so that it would not do anything, but it still went from 2 to 79 now.

Yet I cannot find any script that is causing this

1 Like

Do Ctrl + Shift + F, a bar should pop up and write in the search bar. minutes.Value

1 Like

Only a serverscript and a localscript. The serverscript is the one I disabled, so that cannot be it and localscripts cant change values. It does not give any error message btw.

1 Like

I suggest to do the local script. Use this script as well.
Check the out put, there should be a print with “2” and another right after with a different number

local repl = game:GetService("ReplicatedStorage")
repl.Events.Boosts.Extratime2.OnServerEvent:Connect(function(player)
	if done == false then
		print("Extra Time fired")
		done = true
		player:FindFirstChild("ExtrasBought").Value -= 1
               print(repl.Minutes.Value)
		repl.Minutes.Value += 5 --try repl.Minutes = repl.Minutes + 5
                print(repl.Minutes.Value)
		task.wait(10)
		done = false
	end
1 Like

Daniel, is it possible you could send the full scripts for the script sending and receiving the event

Just discovered it was somewhere in another script. I apologise if I wasted your time.

Have a nice day!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.