Hello! So I have a door script here that the rc (required cash) is supposed to be 5k to enter but the thing is that when someone opens it, every noob can go through that door. I've thought of making something like closing after going through it, but it would close forever so I'm left with the door that lets anyone enter even if they dont have 5k
TL;DR someone with 5k cash goes through door, then everybody can go through that door.
01 | local rc = 5000 |
02 | local debounce = true |
03 | script.Parent.Touched:Connect( function (hit) |
04 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
05 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
06 | if player.leaderstats.Cash.Value > = rc then |
07 | if debounce then |
08 | debounce = false |
09 | script.Parent.Transparency = 0.5 |
10 | script.Parent.CanCollide = false |
11 | if rc.Value < = rc then |
12 | script.Parent.Transparency = 0.15 |
13 | script.Parent.CanCollide = true |
14 | end |
15 | end |
16 | end |
17 | end |
18 | end ) |
You are right, use Local Script. The reason why this does not work as a local script is because you probably put it into the workspace, instead, put it into StarterPlayerScripts and change variables from script.Parent
to workspace:WaitForChild("What you need here")
(Wait for child because it might not replicate when the script runs in starter player)
01 | local rc = 5000 |
02 | local debounce = true |
03 | workspace:WaitForChild( "Door1" ).Touched:Connect( function (hit) |
04 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
05 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
06 | if player.leaderstats.Cash.Value > = rc then |
07 | if debounce then |
08 | debounce = false |
09 | script.Parent.Transparency = 0.5 |
10 | script.Parent.CanCollide = false |
11 | if rc.Value < = rc then |
12 | script.Parent.Transparency = 0.15 |
13 | script.Parent.CanCollide = true |
14 | end |
15 | end |
16 | end |
17 | end |
18 | end ) |
neither this worked
I know this might seem odd, but a pretty simple way to set this up is to use the PhysicsService
to isolate a group of allowed players from the group of disallowed players. If you are aware of how to use the service, that's good. If not, here's a brief explanation of what it does and how useful it can be.
Basic initialization:
1 | local PhysicsService = game:GetService( "PhysicsService" ) |
2 | -- Now you create the collision groups. These can be named anything you want. |
3 | PhysicsService:CreateCollisionGroup( "Name1" ) |
4 | PhysicsService:CreateCollisionGroup( "Name2" ) |
5 | -- etc. |
By creating collision groups you are starting to set up what can collide with something or what is unable to collide with something. The PhysicsService
is very useful for making certain models be able or unable to collide with one another.
So now, because the PhysicsService
only takes BasePart
instances in its collision groups, you need to loop through the player's Character and enter every individual part into a collision group. Something like this, you might think:
1 | for i,v in pairs (player.Character:GetChildren()) do -- player can be defined through something else, like PlayerAdded or GetPlayerFromCharacter() |
2 | if v:IsA( "BasePart" ) then |
3 | PhysicsService:SetPartCollisionGroup(v, "NameofGroup" ) |
4 | end |
5 | end |
The door that you seek to set collidable can be added to the second group, if desired.
Next is a basic function of the service called CollisionGroupSetCollidable(n1, n2, bool)
. This function sets the collision of n1
and n2
to the value of bool
, which must be either true
or false
. I'm pretty sure you can figure this one out by yourself.
Now, I personally don't seek opposition to using a local script, but in my opinion, you should use PhysicsService
over client-sided CanCollide
. It makes your life that much easier.