The random teleport works perfectly, I am just having a hard time with the starter Item.
I want player to go to random place and have this starter gears. I don't want gear in lobby so I did this.
It does work, but sometimes it gives more than 1 set of this item. How do I prevent that?
01 | function onTouched(hit) |
02 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
03 | if player then |
04 | a = math.random( 1 , 4 ) |
05 | if a = = 1 then |
06 | hit.Parent:MoveTo(Vector 3. new(workspace.Tp 1. Position.x,workspace.Tp 1. Position.y+ 8 ,workspace.Tp 1. Position.z)) |
07 | elseif a = = 2 then |
08 | hit.Parent:MoveTo(Vector 3. new(workspace.Tp 2. Position.x,workspace.Tp 2. Position.y+ 8 ,workspace.Tp 2. Position.z)) |
09 | elseif a = = 3 then |
10 | hit.Parent:MoveTo(Vector 3. new(workspace.Tp 3. Position.x,workspace.Tp 3. Position.y+ 8 ,workspace.Tp 3. Position.z)) |
11 | elseif a = = 4 then |
12 | hit.Parent:MoveTo(Vector 3. new(workspace.Tp 4. Position.x,workspace.Tp 4. Position.y+ 8 ,workspace.Tp 4. Position.z)) |
13 | --problem is most likely start here |
14 | game.Lighting:findFirstChild( "ClassicSword" ):clone().Parent = player.Backpack |
15 | game.Lighting:findFirstChild( "HyperlaserGun" ):clone().Parent = player.Backpack |
Use the :FindFirstChild method on the Player's backpack to ensure that the object with the matching name is not there beforehand, like so.
1 | if ( not player.Backpack:FindFirstChild( 'ClassicSword' )) then |
2 | game.Lighting:findFirstChild( "ClassicSword" ):Clone().Parent = player.Backpack |
3 | end |
We use the "not" operator to make the potential "nil" value of the FindFirstChild to be true
, and to make the potential userdata value of "FindFirstChild" to become false
.
Hey!
The touched event doesn't fire just once. It fires multiple times since the character constantly moves as the event fires as a result of physics movement. That explains why the character not only teleports from place to another uncontrollably, but also result in having multiple replicas of gears inside player's backpack.
To prevent them from happening, I suggest adding an extra variable to check if the player is currently touching the teleporter, or check if the gear is already in the inventory. Here's the untested script that I made, and it's personally my method - tell me if something's not correct!
01 | local touchers = { } |
02 | local DELAY = 5 --constant duration in seconds until the part is touchable |
03 |
04 | function onTouched(hit) |
05 | local plr = game.Players:GetPlayerFromCharacter(hit.Parent) |
06 | if plr then --if player owns the character |
07 | local plrTouching = false |
08 | table.foreach(touchers, function (key, val) |
09 | --loop through the table to check if the player is in the list |
10 | if val = = plr then |
11 | plrTouching = true |
12 | end |
13 | end ) |
14 |
15 | if not plrTouching then |
You can also check if the gear is already in player's inventory. Do:
1 | --This is just a reference. |
2 | local player = game.Players.KeanuReeves --a player... or is he? |
3 | if not player.Backpack:FindFirstChild( "ClassicSword" ) then |
4 | --The classic sword is not in the inventory. Perfect! Now we can clone the gear into the backpack. |
5 |
6 | game.Lightning:FindFirstChild( "ClassicSword" ):Clone().Parent = player.Backpack |
7 | end --Done! |