How to create a hitbox that lets players through if they have enough money and keeps them out when they don't have enough? - Roblox

Viewed 57

Been trying for a while now, not sure how to create a way to block players who don't have enough money to enter a zone and let players through who have enough money if not more.

I have a fully transparent part that i would like to act as the hitbox. I also have leaderstats set up and functioning.

If anyone could help, would be much appreciated :)

2 Answers

This is a great use-case for PhysicsService's CollisionGroups.

You can set up a CollisionGroup where objects in one group do not collide with those in another. It's often used for engine optimizations when a lot of objects simply don't need to check whether they need to collide with others, but you can definitely use them for this as well.

For this case, we'll set the door to be in one collision group, and every player that has unlocked the door will be in another. When they touch the door, it will check how much money they have, and if they have enough it will add each part of their character model into the unlocked group. This will allow their character model to phase right through the door.

So, in order to make this work, I added a Script to the Part that I was using for the door, and I added this code :

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

local COST_TO_ENTER = 10 -- update with the actual cost to open the door
local DOOR_GUID = game.HttpService:GenerateGUID()
local LOCKED_DOOR_ID = "LockedDoor" .. DOOR_GUID
local ALLOWED_PLAYERS_ID = "UnlockedPlayers" .. DOOR_GUID

-- find the door that we'll be unlocking
local door = script.Parent

-- create a collision group for the door, and another for players that have unlocked it
PhysicsService:CreateCollisionGroup(LOCKED_DOOR_ID)
PhysicsService:CreateCollisionGroup(ALLOWED_PLAYERS_ID)

-- add the door to the door's collision group
PhysicsService:SetPartCollisionGroup(door, LOCKED_DOOR_ID)

-- make it so that character models in the unlocked group don't collide with the door
PhysicsService:CollisionGroupSetCollidable(LOCKED_DOOR_ID, ALLOWED_PLAYERS_ID, false)


-- listen for when players touch the door to see if we should unlock it
door.Touched:Connect(function(otherPart)
    local player = Players:GetPlayerFromCharacter(otherPart.Parent)
    if player then
        if player.leaderstats.Money.Value >= COST_TO_ENTER then
            -- if they have enough money, add their character model to the allowed player collision group
            for _, part in ipairs(player.Character:GetDescendants()) do
                if part:IsA("BasePart") then
                    PhysicsService:SetPartCollisionGroup(part, ALLOWED_PLAYERS_ID)
                end
            end
            
            -- TODO : update the visuals of the door client-side so that players know that it is unlocked for them
        end
    end 
end)

-- clean up if the door is getting deleted
door.Destroying:Connect(function()
    PhysicsService:RemoveCollisionGroup(LOCKED_DOOR_ID)
    PhysicsService:RemoveCollisionGroup(ALLOWED_PLAYERS_ID)
end)

This system doesn't persist whether a player has unlocked the door. So if your character dies, they may need to have enough money to get back in again. But it would be fairly simple to add a receipt system that keeps track which players have unlocked it, and re-add character models to the collision group upon respawn.

Not Enough Money Enough Money
enter image description here enter image description here

Every time the money changes, you would also change the CanCollide property of the hitbox. For example:

money.Changed:Connect(function()
    hitbox.CanCollide = money.Value < 1024
end)

Here, the < operator will return true if the value of money is smaller than 1024, false otherwise. So if the amount of money is small, the block will collide with the player (not letting him/her in), but otherwise it doesn't.

You would do that in the client so that it only applies to that specific client, not all clients.

Protecting it from cheaters isn't necessary in this case, Roblox characters themselves are exploitable. If you're making an anti-cheat, you can make these zones impossible to enter by teleporting the player to their previous position if they enter when they shouldn't be able to.

Related