This Script Is Not Setting The Cancollide Of A Union To False

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?

So basically the module is asking for a player variable but you’re sending over a nil value.

Add an if statement to check the result of Players:GetPlayerFromCharacter(). It’s also good practice to sanitize values that have the possibility of being nil.

Check that your character resolver is accurate as well.

Yeah, but as a mentioned in the thread, this is a different error which is not causing this problem.

You declared the character variable but never initialised it, setting it to nil.

Now Touched event triggers when anything touches your part (even Baseplate). So all of your if conditions are failing and your character variable remains nil.

Thus, you are effectively calling Players:GetPlayerFromCharacter(nil)

To avoid this, simply add:

if not character then return end

After your if chain.

Edit:

local player = Players:GetPlayerFromCharacter(character)
if not player then return end

This is because of Touched triggers with BasePlate for instance, it will simply return game as an instance.

Yes it is, if the module errors then what makes you think the server handling the module will not error/stop.

Error
Cancollide is not a valid member of UnionOperation “Workspace.castleDoors.Door.Union”

While the union does have a property for CanCollide.

Please note that both C must be capital

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.