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

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

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

✓ Accepted answer

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!

2 comments

0

Thanks! maybe i just didnt mess with events much!

0

Well

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

2 comments

0

Ok ill test it right now! Thanks!

0

i just remembered that i tried it already and it still duplicates

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)

2 comments

0

well the thing is what if they unlock the item in a certain time and not the join time?

0

well, I just dropped a coin in a well

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