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!
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
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:
You don’t have an if before the minutes.
You need to repeat a wait() function until one minute passes.
onTouch is connected to nothing. You need to connect it to a function (i.e function onTouch().
You didn’t add what CanCollide is being modified on.
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)
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”.
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
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)
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.)
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.
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.