local RS = game:GetService("ReplicatedStorage") local remote = RS:WaitForChild("CurrencyIndicator") remote.OnClientEvent:Connect(function(bronzevalue) script.Parent.Text = bronzevalue end)
this is the local script for it
local RS = game:GetService("ReplicatedStorage") local currencyremote = RS:WaitForChild("CurrencyIndicator") local bronzecoin = "BronzeCoin" local silvercoin = "SilverCoin" game:GetService("Players").PlayerAdded:Connect(function(player) local folder = Instance.new("Folder") folder.Name = "CurrencyFolder" folder.Parent = player local bronzevalue = Instance.new("IntValue") bronzevalue.Name = bronzecoin bronzevalue.Parent = folder local silvervalue = Instance.new("IntValue") silvervalue.Name = silvercoin silvervalue.Parent = folder while true do wait(1) bronzevalue.Value = bronzevalue.Value + 10 end local function onChanged() if bronzevalue.Value > 100 then bronzevalue.Value = bronzevalue.Value - 100 silvervalue.Value = silvervalue.Value + 1 end currencyremote:FireClient(player,bronzevalue,silvervalue) end bronzevalue.Changed:Connect(onChanged) end)
so this is my server script and im trying to send the bronzevalue to a local script in StarterGui but for some reason it isnt working and the bronzevalue isnt turning into silvervalue either
The problem is on your server script
. You have a while loop running to add the Bronze. This means that it will never go past the looping as the looping never ends.
To fix this, efficiently, I would thread the while loop
so that it's running in the background and your script can continue.
It would look something like this:
local RS = game:GetService("ReplicatedStorage") local currencyremote = RS:WaitForChild("CurrencyIndicator") local bronzecoin = "BronzeCoin" local silvercoin = "SilverCoin" game:GetService("Players").PlayerAdded:Connect(function(player) local folder = Instance.new("Folder") folder.Name = "CurrencyFolder" folder.Parent = player local bronzevalue = Instance.new("IntValue") bronzevalue.Name = bronzecoin bronzevalue.Parent = folder local silvervalue = Instance.new("IntValue") silvervalue.Name = silvercoin silvervalue.Parent = folder spawn(function() -- begin threading while true do wait(1) bronzevalue.Value = bronzevalue.Value + 10 end end) -- now that the while loop is running in the background, the code can proceed -- and is now able connect the event local function onChanged() if bronzevalue.Value > 100 then bronzevalue.Value = bronzevalue.Value - 100 silvervalue.Value = silvervalue.Value + 1 end currencyremote:FireClient(player,bronzevalue,silvervalue) end bronzevalue:GetPropertyChangedSignal("Value"):Connect(onChanged) -- optimization on Value adjustment detection. end)
From the Wiki:
Having many threads will eventually cause lag in a game, so it's best to keep thread count to a minimum by optimizing code and combining all functionality that can be mixed.
Just like functions, threads take on the environment of the parent context where they were created. So, any existing variables of its parent stack (local or global) will fall into the new thread's environment as well.
Careful not to do this too much.
If you have any questions, please don't hesitate in letting me know.
Kind regards, Rodrigo
~ please accept my answer if this solved your issue.