Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Door script opens and locks doors on different sides of the map??

For some reason, when I press the keybind it opens a door on the other side of the map or the wrong one. Also if you lock one door it locks them all?

Server Script--Inside the door part

local Open = false
local Locked = false
local CanLock = true
function EventFired(Player, PlayerPosition)
	local Magnitude = (script.Parent.Position.Magnitude - PlayerPosition)
	---------------------------OPEN/CLOSE DOOR----------------------------------
	if Magnitude <= 3 and Magnitude >= 0 and Open == false and Locked == false then
		print(Magnitude)
		Open = true
		script.Parent.Transparency = 0.5
		script.Parent.CanCollide = false
		print("Opened Door")
	elseif Magnitude <= 3 and Magnitude >= 0 and Open == true and Locked == false then
		print(Magnitude)
		Open = false
		script.Parent.Transparency = 0
		script.Parent.CanCollide = true
		print("Door Closed")
	end
	---------------------------TRY LOCKED DOOR----------------------------------
	if Locked == true and Open == false and Magnitude <= 3 and Magnitude >= 0 then
		script.Parent.LockedDoor:Play()
	end
end

function Event3Fired(Player, Team, PlayerPosition)
	---------------------------LOCK/UNLOCK DOOR----------------------------------
	local Magnitude = (PlayerPosition - script.Parent.Position.Magnitude)
	if Team == BrickColor.new("Deep orange") and Magnitude <= 3 and Magnitude >= 0 and Locked == false and CanLock == true then
		Locked = true
		script.Parent["Lock/Unlock"]:Play()
	elseif Team == BrickColor.new("Deep orange") and Magnitude <= 3 and Magnitude >= 0 and Locked == true then
		Locked = false
		script.Parent["Lock/Unlock"]:Play()
	elseif Team == BrickColor.new("Really blue") and Magnitude <= 3 and Magnitude >= 0 and Locked == true then
		CanLock = false
		Locked = false
		script.Parent.BreakLock:Play()
	end
end

game:GetService("ReplicatedStorage").Door.OnServerEvent:Connect(EventFired)
game:GetService("ReplicatedStorage").Lock.OnServerEvent:Connect(Event3Fired)

client script

local Player = game.Players.LocalPlayer
function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.E then
		local PlayerPosition = Player.Character.HumanoidRootPart.Position.Magnitude
		game:GetService("ReplicatedStorage").Door:FireServer(PlayerPosition)
	end
end
function onKeyPress2(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.F then
		local PlayerPosition = Player.Character.HumanoidRootPart.Position.Magnitude
		game:GetService("ReplicatedStorage").Lock:FireServer(Player.TeamColor, PlayerPosition)
	end
end
 
game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
game:GetService("UserInputService").InputBegan:Connect(onKeyPress2)

1 answer

You are sending the magnitude which does not make sence since you do PlayerPosition - script.Parent.Position.Magnitude which is not what you want.

I would first do this all on the server side using Player.Character.HumanoidRootPart.Position then take the subtract the positions then get the magnitude.

Example

function EventFired(plr)
	if not plr.Character then return end -- no character
	if not plr.Character.HumanoidRootPart then return end -- best to check all cases
	
	local position = plr.Character.HumanoidRootPart.Position
	local magnitude = (position - script.Parent.Position).Magnitude 

	print('magnitude is', magnitude)
end

I hope this helps.

This discussion is preserved as it was. New answers, comments, and votes are closed.