Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to send value to a local player?

Asked by 4 years ago
Edited 4 years ago
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

0
It doesn't seem to be occurring errors, maybe provide the local script's code. LinavolicaDev 570 — 4y
0
there i edited it jovkon123 17 — 4y
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 Code1400 75 — 4y
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 jovkon123 17 — 4y
0
I edited my answer, hope it helps. LinavolicaDev 570 — 4y

1 answer

Log in to vote
0
Answered by
Wiscript 622 Moderation Voter
4 years ago
Edited 4 years ago

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.

0
You’d want to apply the GetPropertyChangedSignal for optimization on Value adjustment detection. Ziffixture 6913 — 4y
0
fixed Wiscript 622 — 4y
Ad

Answer this question