dbounce = false
PlayerCoins.Changed:Connect(function(NewVal)
if NewVal == OldVal then return end
if dbounce == false then
dbounce = true
print("New Value = ".. NewVal)
print("Old Value = ".. OldVal)
local Difference = NewVal - OldVal
print("Difference = ".. Difference)
-- calculations
EventCoins = EventCoins + Difference
Player.Event.Value = EventCoins -- change value so player can rejoin and see the difference :)
print("Changed ".. Player.Name .."'s Event value from ".. OldVal .." to ".. NewVal .." with int difference ".. Difference ..".")
-- end
OldVal = NewVal -- for next execution, this will be used as the new OldVal.
wait(0.5)
dbounce = false
end
end)
why does it not work as intended? I’m hosting a big event so pelase help with this as fast as possible.
warn("~~Events Loaded~~")
local Players = game:GetService("Players")
local PlayerCharacter = script.Parent
local Player = game:GetService("Players"):FindFirstChild(PlayerCharacter.Name) or Players:GetPlayerFromCharacter(PlayerCharacter.Name)
local PlayerCoins = Player:WaitForChild("leaderstats"):WaitForChild("Coins") -- the players coins
local EventCoinsDS = game:GetService("DataStoreService"):GetOrderedDataStore(game:GetService("ReplicatedStorage"):WaitForChild("EventDataStoreName")) -- gets data from data store
local success,Data = pcall(function()
return EventCoins:GetAsync(Player.UserId)
end)
local Eventcoins = Data
if EventCoins == nil then
EventCoins = 0
end
---
wait(5)
warn("~~Events Started~~")
OldVal = PlayerCoins.Value -- Player's current coins
dbounce = false
PlayerCoins.Changed:Connect(function(NewVal)
if NewVal == OldVal then return end
if dbounce == false then
dbounce = true
print("New Value = ".. NewVal)
print("Old Value = ".. OldVal)
local Difference = NewVal - OldVal
print("Difference = ".. Difference)
-- calculations
EventCoins = EventCoins + Difference
Player.Event.Value = EventCoins -- change value so player can rejoin and see the difference :)
print("Changed ".. Player.Name .."'s Event value from ".. OldVal .." to ".. NewVal .." with int difference ".. Difference ..".")
-- end
OldVal = NewVal -- for next execution, this will be used as the new OldVal.
wait(0.5)
dbounce = false
end
end)
The .Changed event does not return the new value, it returns the PROPERTY that is changed.
I would look into using :GetPropertyChangedSignal(“Value”). This fires when the “Value” property is changed. Unfortunately, this doesn’t return the “new value”, so you will have to change that yourself.
dbounce = false
PlayerCoins:GetPropertyChangedSignal("Value"):Connect(function()
local NewVal = PlayerCoins.Value
if NewVal == OldVal then return end
if dbounce == false then
dbounce = true
print("New Value = ".. NewVal)
print("Old Value = ".. OldVal)
local Difference = NewVal - OldVal
print("Difference = ".. Difference)
-- calculations
EventCoins = EventCoins + Difference
Player.Event.Value = EventCoins -- change value so player can rejoin and see the difference :)
print("Changed ".. Player.Name .."'s Event value from ".. OldVal .." to ".. NewVal .." with int difference ".. Difference ..".")
-- end
OldVal = NewVal -- for next execution, this will be used as the new OldVal.
wait(0.5)
dbounce = false
end
end)
.Changed event in THIS CASE (or any other case that has a Value instance such as Int/String/Bool/Number ect.) DOES return the new value, in any other case you would be right.
This error is caused because you are changing the item in a .Changed event which will fire itself over and over etc, i did NOT read the script, i just said the most common reason
i don’t know if this information is useful by the way, but might it have anything to do with the fact that there are several of these scripts? Like every startercharacterscript gets clones into each player? I know they shouldn’t overlap as each script has control over one players’ value, but still.
I have one alternative to this whole issue and that is to delete the entire code and find all scripts that changes the coin value, then change the events value as well on the line below that.
dbounce = false
PlayerCoins.Changed:Connect(function(NewVal)
if NewVal == OldVal then return end
if dbounce == true then return end
dbounce = true
delay(.5 , function()
dbounce = false
end)
print("New Value = ".. NewVal)
print("Old Value = ".. OldVal)
local Difference = NewVal - OldVal
print("Difference = ".. Difference)
-- calculations
EventCoins = EventCoins + Difference Player.Event.Value = EventCoins -- change value so player can rejoin and see the difference :)
print("Changed ".. Player.Name .."'s Event value from ".. OldVal .." to ".. NewVal .." with int difference ".. Difference ..".")
-- end
OldVal = NewVal -- for next execution, this will be used as the new OldVal.
end)