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

How to I prevent duplicate give to character?

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?

function onTouched(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
	a = math.random(1,4)
	if a == 1 then
	hit.Parent:MoveTo(Vector3.new(workspace.Tp1.Position.x,workspace.Tp1.Position.y+8,workspace.Tp1.Position.z))
	elseif a == 2 then
	hit.Parent:MoveTo(Vector3.new(workspace.Tp2.Position.x,workspace.Tp2.Position.y+8,workspace.Tp2.Position.z))
	elseif a == 3 then
	hit.Parent:MoveTo(Vector3.new(workspace.Tp3.Position.x,workspace.Tp3.Position.y+8,workspace.Tp3.Position.z))
	elseif a == 4 then
	hit.Parent:MoveTo(Vector3.new(workspace.Tp4.Position.x,workspace.Tp4.Position.y+8,workspace.Tp4.Position.z))
	--problem is most likely start here
game.Lighting:findFirstChild("ClassicSword"):clone().Parent = player.Backpack
game.Lighting:findFirstChild("HyperlaserGun"):clone().Parent = player.Backpack
game.Lighting:findFirstChild("Pistol"):clone().Parent = player.Backpack
game.Lighting:findFirstChild("RocketLauncher"):clone().Parent = player.Backpack 
	end
    end
end
 
script.Parent.Touched:connect(onTouched)

2 answers

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

if (not player.Backpack:FindFirstChild('ClassicSword')) then
	game.Lighting:findFirstChild("ClassicSword"):Clone().Parent = player.Backpack
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.

2 comments

0

upvoet bples

1

ah i wishhhh cuz your answer was flawless

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!

local touchers = {}
local DELAY = 5 --constant duration in seconds until the part is touchable

function onTouched(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr then --if player owns the character
        local plrTouching = false
        table.foreach(touchers, function(key, val)
            --loop through the table to check if the player is in the list
            if val == plr then
                plrTouching = true
            end
        end)
        
        if not plrTouching then
            table.insert(touchers, plr) --add player to pending list
            -- do your thing blah blah

            wait(DELAY) --wait until timeout, then remove the player from the pending list
            for i = #touchers, 1, -1 do
                if touchers[i] == plr then
                    table.remove(t, i)
                end
            end
        end
    end
end)

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

--This is just a reference.
local player = game.Players.KeanuReeves --a player... or is he?
if not player.Backpack:FindFirstChild("ClassicSword") then
    --The classic sword is not in the inventory. Perfect! Now we can clone the gear into the backpack. 
    
    game.Lightning:FindFirstChild("ClassicSword"):Clone().Parent = player.Backpack
end --Done!

4 comments

0

Bold of you to assume Keanu Reeves is simply a player.

0

Oops, my bad.

0

Line 22 (t) what is that? shia labeouf?

0

table.remove(allLivingOrganisms, me)

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