Alright, so I am trying to make a door that opens whenever a player touched the door. I have a ModuleScript and a Server Script.
Server Script in Server Script Service
local distanceModule = require(game.ServerScriptService.doorHandlers.getDistance)
local doorFolder = game.Workspace.castleDoors
local collectionService = game:GetService("CollectionService")
local currentTag = 1
for _, model in ipairs(doorFolder:GetChildren()) do
collectionService:AddTag(model.Union, "doorUnions")
currentTag += 1
end
for _, part in pairs(collectionService:GetTagged("doorUnions")) do
part.Touched:Connect(function(hit)
local character
if hit:IsA("Accessory") then
character = hit.Parent.Parent
elseif not hit:IsA("Accessory") then
character = hit.Parent
end
if character then
local result = distanceModule:getPlayerDistance(game.Players:GetPlayerFromCharacter(character), part)
if result == true then
part.Cancollide = false
end
end
end)
end
ModuleScript
local module = {}
function module:getPlayerDistance(player: Player, doorModel)
local playerCharacter = player.Character or player.CharacterAdded:Wait()
if playerCharacter then
if (Vector3.new(doorModel.CFrame.X, doorModel.CFrame.Y, doorModel.CFrame.Z) - Vector3.new(playerCharacter.HumanoidRootPart.CFrame.X,playerCharacter.HumanoidRootPart.CFrame.Y,playerCharacter.HumanoidRootPart.CFrame.Z)).Magnitude <= 3 then
return true
else
return false
end
end
end
return module
Errors
ServerScriptService.doorHandlers.getDistance:4: attempt to index nil with ‘Character’
The errors are not constantly printing I will fix this issue later, but I thought I should still include this error.
Can you solve the problem?