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

How to make a part's cancollide be false for one player when they have collected enough of points?

Asked by 5 years ago

Hi, I want to make a game where you need to collect these tokens, when you have enough tokens, you get to go further through the game, how can I make sure I have a door that only works for the people that have collected enough tokens?

0
If your game is FE, you can simply set CanCollide to false from a LocalScript. Otherwise you need to use Collision Groups Amiaa16 3227 — 5y

2 answers

Log in to vote
2
Answered by
hellmatic 1523 Moderation Voter
5 years ago
Edited 5 years ago

You can use CollisionFiltering

Server script: (place in workspace)

local PhysicsService = game:GetService("PhysicsService")

local doorGroup = "doorGroup"
local accessGroup = "accessGroup"

--you can only create a collision group once. If it already exists it will return a error
PhysicsService:CreateCollisionGroup(doorGroup)
PhysicsService:CreateCollisionGroup(accessGroup)

PhysicsService:CollisionGroupSetCollidable(doorGroup, accessGroup, false) -- makes the two groups uncollidable with eachother

Place this script inside the door:

local Players = game:GetService('Players')
local PhysicsService = game:GetService('PhysicsService')

local AccessList = {}

local Door = script.Parent
local RequiredTokens = 10

PhysicsService:CreateCollisionGroup(Door, "doorGroup") -- set the Door's group

function TOUCHED(hit)
    if hit then 
        if Players:FindFirstChild(hit.Parent.Name) then 
        local player = Players:FindFirstChild(hit.Parent.Name)
        local Tokens = player:FindFirstChild("leaderstats').Tokens -- replace
        if not CHECK_LIST(player.Name) and (Tokens.Value >= 10) then 
            local character = player.Character
            ACCESS(character)
        end
    end
end

function ACCESS(character)
    if character then 
        for _, v in pairs(character:GetChildren()) do 
            if v:IsA('BasePart') then 
                PhysicsService:SetPartCollisionGroup(v, "accessGroup")
                table.insert(AccessList, character.Name) -- add player to access group
            end
        end
    end
end

function CHECK_LIST(playerName) -- checks if the player is in the access list/not
    for i = 1, #AccessList do 
        if (playerName == AccessList[i]) then 
            return true 
        else
            return false 
        end
    end
end

Door.Touched:connect(TOUCHED)
0
A few errors cause of some little mistakes but it should work, thanks. aoaoaoaoa00 4 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
num = -- Specified number of maximum points
plr = game.Players:GetPlayerFromCharacter(script.Parent)
stats = plr:FindFirstChild("leaderstats") -- Find the leaderboard
points = stats:FindFirstChild("Points") -- Find the Points value
debounce = true
script.Parent.Touched:Connect(function()
    debounce = false
    if plr then
        if stats and points then
            if points.Value == num then -- If maximum is reached...
                script.Parent.CanCollide = false -- Turn CanCollide off
            end
        end
    end
    wait(1)
    debounce = true
end)
0
Why aren't you using any local variables? And if its a LocalScript, why not just use LocalPlayer? And if its a serverscript... then its very bad Amiaa16 3227 — 5y
0
Not a fan of local variables. I use variables based on raw Lua code. DeceptiveCaster 3761 — 5y
0
If it works, I'll take it, thanks. aoaoaoaoa00 4 — 5y
0
oof i made a answer but you posted yours first ): hellmatic 1523 — 5y
0
lol DeceptiveCaster 3761 — 5y

Answer this question