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:
1 | game.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 |
8 | end ) |
Thanks!
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 :
01 | local 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 |
07 | end |
08 |
09 | local 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 |
15 | end |
16 |
17 | game.Players.PlayerAdded:Connect(Check) -- Executes our check function |
Lemme know if it helps!
01 | game.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 |
11 | 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
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.
1 | wait( 5 ) --// Allows the player to get a chance to load in |
2 | game.Players.PlayerAdded:Connect( function (Player) |
3 | local newValue = Instance.new( "StringValue" ) |
4 | newValue.Name = "nameHere" |
5 | newValue.Parent = Player.Backpack |
6 | end ) |