After the player plays for certain minutes, they can go into a door. Well, the door won't work. Help!

I am a newbie scripter so I am still understanding and trying to script. When the player reaches a certain time, lets say 1 minute, I want them to be able to access a door. I already have the minutes played script written, but do I put

minutes == 1 then
game.Workspace.Door.Touched:Connect(onTouch)
 CanCollide = true
end

The part name is door. Help will be greatly appreciated as I am a beginner. Thanks!

1 Like

your script doesn’t make sense, I will make a beginner script of what I think you want to do:


local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local JoinTime = os.time()

workspace.Door.Touched:Connect(function(hit)
        hit = game.Players:GetPlayerFromCharacter(hit.Parent)
        if hit ~= Player then return end
        if os.time() - JoinTime => 60*1 then -- Change 1 to the minutes you want
               workspace.Door.CanCollide = false
               wait(2)
               workspace.Door.CanCollide = true
        end
end)

I don’t tested this script, it can error, put it in a LocalScript in StarterPlayerScripts

1 Like

Here:

repeat wait(1) time = time + 1 until time == 60
workspace.Door.Touched:Connect(function()
      workspace.Door.CanCollide = false
end)

Here are the problems with your script:

  1. You don’t have an if before the minutes.
  2. You need to repeat a wait() function until one minute passes.
  3. onTouch is connected to nothing. You need to connect it to a function (i.e function onTouch().
  4. You didn’t add what CanCollide is being modified on.
  5. CanCollide is being set to true!

You will need to use a LocalScript to change the CanCollide of a Door just for that player, otherwise after one minute, any player can also walk through.
In that case:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

delay(60, function()
      workspace.Door.CanCollide = false
end)
1 Like

You don’t assigned time, and it will error

1 Like

I made the assumption that he will know how to create a variable called time.

2 Likes

If he is a beginner in scripting, you should want to use relatively basic code so that they can understand it and learn from it.

If you use code that is unnecessarily intricate, they will not learn from your code, won’t understand it and therefore, your code won’t benefit them in the long run.

Also, there is no need to wait for the Character to load, seeing as you don’t even use it. Secondly, you don’t need to do 60 x 1 since 60 x 1 = 60 (instead, just do 60). Thirdly, there is no need to wait for the Player to touch the door, you will create an unnecessary amount of Touched events and you only need to wait that minute for the door to “unlock”.

3 Likes

Thank you so much. I am going to test it out now.

1 Like

The code you provided doesn’t show a whole lot about the time process, but rather the door opening. In your script, you also make the door solid instead of walk-through. I will provide some basic code to show how this could be achieved in the simplest way possible.

This should be a LocalScript in StarterPlayerScripts, or anywhere where it will run except StarterCharacterScripts.

local minutes = 60*1 --The "1" would be the number of minutes

wait(minutes) --Yield the script for the amount of minutes
--Declare the touched event
door.Touched:Connect(function() --Replace "door" with the door in the game
    door.CanCollide = false
end
1 Like

I’m going to try to break down a script.

minutes = 60
wait(minutes) -- This will wait 60 seconds before continuing with the script.

Theblock = game.Workspace.Part --Change this to the part that you have.

Theblock.Touched:Connect(function(h) -- Whenever the block is Touched, run the code below.
-- the h in parenthesis represents the thing that touched Theblock.
-- It is a variable, so you can change "h" to whatever name you want, and change it everywhere else it is mentioned.

if game.Players:GetPlayerFromCharacter(h.Parent) then -- If the part that touched it is a player then. (Kind of complicated)

Theblock.CanCollide = false -- Change the CanCollide property to false, which means things can go through it.

end
end)

2 Likes

This is a bit of a nitpick, but wait(1) is not guaranteed to wait exactly one second. I’d use an os.time based approach or add the delta time that wait(1) returns instead of 1. (wait returns how long it actually waited.)

5 Likes

Your script works! Thank you! But I want to change the minutes so I want the door to open at 1 hour.

minutes = 3600
wait (minutes)

does that mean the door’s cancollide will be false after 3600 seconds?
if so I am assuming I don’t need to change anything else.

Yes, also instead of this:

local mins = 3600
wait( mins )

You can just do this:

wait( 3600)

It’s shorter code that way.

I want to add something to help you out in the future. You’ll notice that other players may be able to go into this door as well. If this is something you intend, you can ignore. If not, I recommend moving this to a local script, to ensure only the player you want can go through the door.

1 Like

I added the local script into starterplayerscripts. so will other players get through as well?

No, if it is in a local script it will only take place on the client (The player.) And not the server ( Everyone )

Here’s a great tutorial on the client server model: Client-Server Runtime | Documentation - Roblox Creator Hub

No, other players will not get through.

Roblox has something cool called Filtering Enabled. Let me draw you a diagram.

So whatever occurs on the server, will be replicated to the clients. However, what occurs on the client, is not replicated to the client. As you see, if you want to have a client change replicate to the server, you can do so with a remote event.

This might be a bit advanced for your needs though, and I’m pretty sure your question in here has been answered by other people at this point.

Happy to help none the less. If you want further assistance with making said door filtering enabled, let me know.