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

[Solved by "BestCreativeBoy"] How to prevent instanse.new from duplicating?

Asked by 4 years ago
Edited 4 years ago

so im making this so when u have a certain thing it puts a stringValue in ur player it works but the thing is since its a while true do it duplicates ive tried many ways but it sometimes doesnt work or doesnt even insert

code:

1game.Players.PlayerAdded:Connect(function(player)
2    while true do
3        wait(3)
4            local newValue = Instance.new("StringValue")
5            newValue.Name = "nameHere"
6            newValue.Parent = player.Backpack
7    end
8end)

Thanks!

3 answers

Log in to vote
1
Answered by 4 years ago

Well, the code you provided is meant to create a new string value every 3 seconds. So, it doesn't make any sense with the code you provided and the question you are asking.

I assume that you trying to add string value when player has a certain item, eg : tool, in the backpack. Well, instead of using while loop you can use ChildAdded and if statements to check whether the item you were expecting is added.

Here's an example :

01local function Checker(itemAdded) -- Custom function, itemAdded is a variable assigned to the item that is added
02    if itemAdded:IsA('Tool') then -- Checks if the added item is a tool
03        if itemAdded.Name == 'Test' then -- If the name of the itemAdded is Test
04            print('It worked!') -- It will print 'It worked!' in the output.
05        end
06    end
07end
08 
09local function Check(player) -- Our custom function
10    repeat wait() until player.Character -- It won't execute the function until whole character is loaded
11    if player.Backpack then -- If backpack of the player is found
12        local backpack = player.Backpack -- Assigning variable to the backpack
13        backpack.ChildAdded:Connect(Checker) -- Executes our Checker function we created above, when something is added to backpack.
14    end
15end
16 
17game.Players.PlayerAdded:Connect(Check) -- Executes our check function

Lemme know if it helps!

0
Thanks! maybe i just didnt mess with events much! Rgboffical_yt 40 — 4y
0
Well DiamondComplex 285 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
01game.Players.PlayerAdded:Connect(function(player)
02    while true do
03        wait(3)
04        if Player.Backpack:FindFirstChild("nameHere") then
05        else
06            local newValue = Instance.new("StringValue")
07            newValue.Name = "nameHere"
08            newValue.Parent = player.Backpack
09        end
10    end
11end)

This should work. If it did, click accept. If it did not or I misinterpreted your question make sure to let me know! It might not be exactly like this, but something like this should work. If you don't know I suggest learning about FindFirstChild, its very useful

0
Ok ill test it right now! Thanks! Rgboffical_yt 40 — 4y
0
i just remembered that i tried it already and it still duplicates Rgboffical_yt 40 — 4y
Log in to vote
0
Answered by
0Vyp 14
4 years ago
Edited 4 years ago

The player will never have a tool in their backpack when they first join the game unless its in starter pack.Therefore you will just need to create one since there will be nothing to find.

1wait(5) --// Allows the player to get a chance to load in
2game.Players.PlayerAdded:Connect(function(Player)
3    local newValue = Instance.new("StringValue")
4    newValue.Name = "nameHere"
5    newValue.Parent = Player.Backpack
6end)
0
well the thing is what if they unlock the item in a certain time and not the join time? Rgboffical_yt 40 — 4y
0
well, I just dropped a coin in a well DiamondComplex 285 — 4y

Answer this question