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.
local rc = 5000 local debounce = true script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player.leaderstats.Cash.Value >= rc then if debounce then debounce = false script.Parent.Transparency = 0.5 script.Parent.CanCollide = false if rc.Value <= rc then script.Parent.Transparency = 0.15 script.Parent.CanCollide = true end end end end 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)
local rc = 5000 local debounce = true workspace:WaitForChild("Door1").Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player.leaderstats.Cash.Value >= rc then if debounce then debounce = false script.Parent.Transparency = 0.5 script.Parent.CanCollide = false if rc.Value <= rc then script.Parent.Transparency = 0.15 script.Parent.CanCollide = true end end end end 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:
local PhysicsService = game:GetService("PhysicsService") -- Now you create the collision groups. These can be named anything you want. PhysicsService:CreateCollisionGroup("Name1") PhysicsService:CreateCollisionGroup("Name2") -- 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:
for i,v in pairs(player.Character:GetChildren()) do -- player can be defined through something else, like PlayerAdded or GetPlayerFromCharacter() if v:IsA("BasePart") then PhysicsService:SetPartCollisionGroup(v, "NameofGroup") end 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.