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:

game.Players.PlayerAdded:Connect(function(player)
    while true do
        wait(3)
            local newValue = Instance.new("StringValue")
            newValue.Name = "nameHere"
            newValue.Parent = player.Backpack
    end
end)

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 :

local function Checker(itemAdded) -- Custom function, itemAdded is a variable assigned to the item that is added
    if itemAdded:IsA('Tool') then -- Checks if the added item is a tool
        if itemAdded.Name == 'Test' then -- If the name of the itemAdded is Test
            print('It worked!') -- It will print 'It worked!' in the output. 
        end
    end
end

local function Check(player) -- Our custom function
    repeat wait() until player.Character -- It won't execute the function until whole character is loaded
    if player.Backpack then -- If backpack of the player is found
        local backpack = player.Backpack -- Assigning variable to the backpack
        backpack.ChildAdded:Connect(Checker) -- Executes our Checker function we created above, when something is added to backpack.
    end
end 

game.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
game.Players.PlayerAdded:Connect(function(player)
    while true do
        wait(3)
        if Player.Backpack:FindFirstChild("nameHere") then
        else
            local newValue = Instance.new("StringValue")
            newValue.Name = "nameHere"
            newValue.Parent = player.Backpack
        end
    end
end)

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.

wait(5) --// Allows the player to get a chance to load in
game.Players.PlayerAdded:Connect(function(Player)
    local newValue = Instance.new("StringValue")
    newValue.Name = "nameHere"
    newValue.Parent = Player.Backpack
end)
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