Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to send value to a local player?

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

5 comments

0

It doesn't seem to be occurring errors, maybe provide the local script's code.

0

there i edited it

0

any code underneath the while loop will not run, place onChanger() above the while loop, im not sure if that applies to functions that are called out side of the while loops scope but i know it does for regular codes and if statements. also send your stuff to PlayerGui instead. everything thats in starter gui will be replicated to player gui when the game starts so if you're looking to change stuf

0

im not sending it directly there its requiered by a remote event so it will be able to change in game also mb havent scripted in a while so i forgot abt the while loop soz ty tho

0

I edited my answer, hope it helps.

1 answer

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.

2 comments

0

You’d want to apply the GetPropertyChangedSignal for optimization on Value adjustment detection.

0

fixed

This discussion is preserved as it was. New answers, comments, and votes are closed.