Hello! This is a relatively simple topic but I seem to have an error within my keycard script where it should simply detect if the object hitting the door is the “Visa” which I have made and will set the door’s CanCollide to false.
The Code:
local function onTouch(hit)
if hit.Parent.Name == "Visa" then
script.Parent.CanCollide = false
end
end
script.Parent.Touched:Connect(onTouch)
It is a local function as I only want the door to open for one player.
It works now, it was my fault. In order to obtain the card you it was cloned from replicated storage from a local script, so the server script couldn’t detect it. I am sorry for wasting everyone’s time.
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then -- verifys if its a player Note only players has humanoids
if hit.Parent.Name == "Visa" then
script.Parent.CanCollide = false
end
end
end)
Using Local Script to do something with server side is not possible. Local Script can’t clone something what’s inside ServerStorage. I suggest moving Doors to ReplicatedStorage and cloning it from there
--LocalScript inside StarterPack/StarterGui/StarterPlayer
local Door = game.ReplicatedStorage.Door:Clone()
local function onTouch(hit)
if hit.Parent.Name == "Visa" then
Door.CanCollide = false
end
end
Door.Touched:Connect(onTouch)
If you use only one doors do this inside local script:
local function onTouch(hit)
if hit.Parent.Name == "Visa" then
game.Workspace.Door.CanCollide = false
end
end
game.Workspace.Door.Touched:Connect(onTouch)
The replicated storage option is if you want to use many doors.
The script is in StarterPack because It’s client part of Services It could have been in StarterGui or any other client part of Services.