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

How do I make it so a brick can only be touched once by each player?

Asked by 9 years ago

I don't know where to start...can anyone help?

2 answers

Log in to vote
0
Answered by 9 years ago

To do this, you will need to have a value inserted into the player. I would use a BoolValue and have it be inside the player's Backpack

function touch(hit)
local humanoid=hit.Parent:findFirstChild("Humanoid")
    if humanoid~=nil then
        local user = game.Players:GetPlayerFromCharacter(hit.Parent)
        if user.Backpack.Contacted.Value ~= true then ---Change "Contacted" to whatever you want to call the value
        ----insert code here
        end
    end
end

script.Parent.Touched:connect(touch)
Ad
Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

There's many ways you could do this. The easiest way is to use a BoolValue object as an indicator.

-- if "script.Parent" is the one that gets touched.
Block = script.Parent

Block.Touched:connect(function(hit)
    local HumanoidIsFound = hit.Parent and hit.Parent:FindFirstChild("Humanoid")
    local PlayerIsFound = game.Players:GetPlayerFromCharacter(hit.Parent)
    -- Some prerequisites
    if HumanoidIsFound and PlayerIsFound then   -- if the thing that touched the block is a player, then
        if not PlayerIsFound:FindFirstChild("IsTouchedOnce") then
            local BoolVal = Instance.new("BoolValue", PlayerIsFound)
            BoolVal.Name = "IsTouchedOnce"
            BoolVal.Value = true
            -- Insert the rest of the code here
        end
    end
end)
-- This will insert a boolean value object inside the player instance. That means that the block will be touched only once in the game, no matter how many times the player resets its character.
-- To avoid this, on line 9 & 10, replace "PlayerIsFound" with "hit.Parent".

If you want to have fun with scripting, you can add an entry to a table every time a new player touches the block.

You'll need a function that verifies whether or not the name is already recorded in the function. To do this, you'll need a "for" loop, to scan each value of the table and see if the name is equivalent to the person that touched the block.

PlayerTable = {}

function VerifyPlayer(plr)
    for i, v in pairs(PlayerTable) do
        if v == plr.Name then
            return true
        end
    end
    return false
end

But the rest of the code is similar to the first option.

Block = script.Parent

Block.Touched:connect(function(hit)
    local HumanoidIsFound = hit.Parent and hit.Parent:FindFirstChild("Humanoid")
    local PlayerIsFound = game.Players:GetPlayerFromCharacter(hit.Parent)
    -- Some prerequisites
    if HumanoidIsFound and PlayerIsFound then
        if not VerifyPlayer(PlayerIsFound) then
            table.insert(PlayerTable, (#PlayerTable + 1), PlayerIsFound.Name)
            -- Insert the rest of the code here
        end
    end
end)

-- I'm not sure about line 9. Please verify if this is correct.

Answer this question