im having trouble with this script which i made another dev forum post about but it was about another glitch.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
script.Parent.Touched:Connect(function(hit)
RunService.Heartbeat:Connect(function()
local plr = Players:GetPlayerFromCharacter(hit.Parent)
if script.Parent.Parent.door.CFrame == script.Parent.Parent.door.open.CFrame then
plr.Character.hidden.Value = 1
end
if script.Parent.Parent.door.CFrame == script.Parent.Parent.door.close.CFrame then
plr.Character.hidden.Value = 2
end
end)
end)
script.Parent.TouchEnded:Connect(function(hit)
local plr = Players:GetPlayerFromCharacter(hit.Parent)
plr.Character.hidden.Value = 0
end)
basically the value doesnt become 0 when the player leaves the part, i was told to use region3 or a spatial quey but idk that is so if anyone can help with those tell me, tried gettouchingparts() but it didnt work
Hello there! There are several issues with your script:
You are using HeartBeat for no reason (and because of that, the value will always be 1 or 2 as it keeps constantly changing it even if the player did not touch the object).
You are not using variables.
You should use attributes in your case, or some similar logic.
I don’t know where you got this code from but its horrible, I recommend asking AI for help it can help with simple stuff like this.
You have no debounce logic.
Here is an example that should work:
local Players = game:GetService("Players")
local sensor = script.Parent
local door = workspace:WaitForChild("Door")
local touching = false
sensor.Touched:Connect(function(hit)
if touching or hit.Name ~= "HumanoidRootPart" then
return
end
touching = true
local character = hit.Parent
if door:GetAttribute("open", true) then
character:SetAttribute("hidden", 1)
else
character:SetAttribute("hidden", 2)
end
print(character:GetAttribute("hidden"))
end)
sensor.TouchEnded:Connect(function(hit)
if touching == false or hit.Name ~= "HumanoidRootPart" then
return
end
touching = false
local character = hit.Parent
character:SetAttribute("hidden", 0)
print(character:GetAttribute("hidden"))
end)
Then just use GetAttributeChangedSignal(). However, there is probably a better solution (it would also be more complicated) but I would need to see the script that changes the door state.
local y = false
local TweenService = game:GetService("TweenService")
script.Parent.MouseClick:Connect(function()
if y == false then
y = true
TweenService:Create(script.Parent.Parent.Parent.door, TweenInfo.new(1), {CFrame = script.Parent.Parent.open.CFrame}):Play()
elseif y == true then
y = false
TweenService:Create(script.Parent.Parent.Parent.door, TweenInfo.new(1), {CFrame = script.Parent.Parent.close.CFrame}):Play()
end
end)
Thanks, here is an example script I made that should handle everything, and all you have to do is put all doors and sensors in a folder. Also make sure your doors have handles and a weldconstraint that has these properties:
Part0: the handle
Part1: the door
The door should be unanchored, the handle should be anchored. The sensor should have cancollide set to false.
Here is the script:
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(1)
local Doors = workspace:WaitForChild("Doors"):GetChildren()
local Sensors = workspace:WaitForChild("Sensors"):GetChildren()
local activePlayers = {}
Players.PlayerAdded:Connect(function(player)
player:SetAttribute("hidden", 0)
end)
for i, v in ipairs(Doors) do
local door = v:WaitForChild("door")
local handle = v:WaitForChild("handle")
local clickDetector = door:WaitForChild("ClickDetector")
local sensor = Sensors[i]
local closedCFrame = handle.CFrame
local openCFrame = handle.CFrame * CFrame.Angles(0, -90, 0)
clickDetector.MouseClick:Connect(function(player)
if door:GetAttribute("open", true) then
door:SetAttribute("open", false)
if player:GetAttribute("hidden") == 1 and activePlayers[player] == sensor then
player:SetAttribute("hidden", 2)
end
TweenService:Create(handle, tweeninfo, {CFrame = closedCFrame}):Play()
else
door:SetAttribute("open", true)
if player:GetAttribute("hidden") == 2 and activePlayers[player] == sensor then
player:SetAttribute("hidden", 1)
end
TweenService:Create(handle, tweeninfo, {CFrame = openCFrame}):Play()
end
print(player:GetAttribute("hidden"))
end)
sensor.Touched:Connect(function(hit)
if hit.Name ~= "HumanoidRootPart" then
return
end
local player = Players:GetPlayerFromCharacter(hit.Parent)
activePlayers[player] = sensor
if door:GetAttribute("open", true) then
player:SetAttribute("hidden", 1)
else
player:SetAttribute("hidden", 2)
end
print(player:GetAttribute("hidden"))
end)
sensor.TouchEnded:Connect(function(hit)
if hit.Name ~= "HumanoidRootPart" then
return
end
local player = Players:GetPlayerFromCharacter(hit.Parent)
activePlayers[player] = nil
player:SetAttribute("hidden", 0)
print(player:GetAttribute("hidden"))
end)
end
And I attached an example place to this message aswell. doorsystem.rbxl (60.0 KB)
IMPORTANT:
The sensors and doors must be in order in the hierarchy however if you want, you can number the sensors and change this line: local sensor = Sensors[i]