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

How to I prevent duplicate give to character?

Asked by 5 years ago

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?

01function 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(Vector3.new(workspace.Tp1.Position.x,workspace.Tp1.Position.y+8,workspace.Tp1.Position.z))
07    elseif a == 2 then
08    hit.Parent:MoveTo(Vector3.new(workspace.Tp2.Position.x,workspace.Tp2.Position.y+8,workspace.Tp2.Position.z))
09    elseif a == 3 then
10    hit.Parent:MoveTo(Vector3.new(workspace.Tp3.Position.x,workspace.Tp3.Position.y+8,workspace.Tp3.Position.z))
11    elseif a == 4 then
12    hit.Parent:MoveTo(Vector3.new(workspace.Tp4.Position.x,workspace.Tp4.Position.y+8,workspace.Tp4.Position.z))
13    --problem is most likely start here
14game.Lighting:findFirstChild("ClassicSword"):clone().Parent = player.Backpack
15game.Lighting:findFirstChild("HyperlaserGun"):clone().Parent = player.Backpack
View all 22 lines...

2 answers

Log in to vote
1
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago

Use the :FindFirstChild method on the Player's backpack to ensure that the object with the matching name is not there beforehand, like so.

1if (not player.Backpack:FindFirstChild('ClassicSword')) then
2    game.Lighting:findFirstChild("ClassicSword"):Clone().Parent = player.Backpack
3end

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.

0
upvoet bples Fifkee 2017 — 5y
1
ah i wishhhh cuz your answer was flawless Afterl1ght 321 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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!

01local touchers = {}
02local DELAY = 5 --constant duration in seconds until the part is touchable
03 
04function 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
View all 27 lines...

You can also check if the gear is already in player's inventory. Do:

1--This is just a reference.
2local player = game.Players.KeanuReeves --a player... or is he?
3if 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
7end --Done!
0
Bold of you to assume Keanu Reeves is simply a player. Fifkee 2017 — 5y
0
Oops, my bad. Afterl1ght 321 — 5y
0
Line 22 (t) what is that? shia labeouf? Asher0606 36 — 5y
0
table.remove(allLivingOrganisms, me) Afterl1ght 321 — 5y

Answer this question