I’m making a game similar to Doors and want to create a mechanism where you can hide in a wardrobe.
However, even though the server logs that the player is in the wardrobe, it decides to kill them because they supposedly aren’t.
The code is as follows:
rushPart.Touched:Connect(function(touch)
local playerTouching = playerService:GetPlayerFromCharacter(touch.Parent)
if playerTouching then
if playerTouching == player then
local inWardrobe = false
for i = 1, 6, 1 do
light.PointLight.Brightness = 0
task.wait(0.05)
light.PointLight.Brightness = 0.38
task.wait(0.05)
end
task.wait(5)
wardrobePart.Touched:Connect(function(wardrobeTouch)
local playerWardrobeTouching = playerService:GetPlayerFromCharacter(wardrobeTouch.Parent)
if playerWardrobeTouching then
if playerWardrobeTouching == player then
inWardrobe = true
end
end
end)
if inWardrobe == false then
player.Character.Humanoid.Health = 0
end
light.PointLight.Brightness = 0
end
end
end)
It’s a LocalScript in StarterPlayerScripts. Any tips?
Hi, the reason is most probably the ‘task.wait(5)’ you have which prevents the ‘Touched’ event from firing in the first place, you can use spawn() to make a separate thread to solve this issue;
rushPart.Touched:Connect(function(touch)
local playerTouching = playerService:GetPlayerFromCharacter(touch.Parent)
if playerTouching then
if playerTouching == player then
local inWardrobe = false
spawn(function()
for i = 1, 6, 1 do
light.PointLight.Brightness = 0
task.wait(0.05)
light.PointLight.Brightness = 0.38
task.wait(0.05)
end
task.wait(5)
end)
wardrobePart.Touched:Connect(function(wardrobeTouch)
local playerWardrobeTouching = playerService:GetPlayerFromCharacter(wardrobeTouch.Parent)
if playerWardrobeTouching then
if playerWardrobeTouching == player then
inWardrobe = true
end
end
end)
if inWardrobe == false then
player.Character.Humanoid.Health = 0
end
light.PointLight.Brightness = 0
end
end
end)
I would rewrite this entire code if I were you, quite a lot could be better efficiency and readability wise, here’s a basic example of a better system for you:
make sure this code is inside of a ServerScript
game.Players.PlayerAdded:Connect(function(player) -- sets up every new character with the IsHiding attribute to avoid errors
player.CharacterAdded:Connect(function(character)
character:SetAttribute("IsHiding", false)
end)
end)
rushPart.Touched:Connect(function(otherPart)
if otherPart:FindFirstChild("HumanoidRootPart") then
local characterWhoTouchedPart = otherPart.Parent
if not characterWhoTouchedPart:GetAttribute("IsHiding") then -- if the player is NOT hiding then...
-- run this code
end
end
end)
wardropePart.Touched:Connect(function(otherPart)
if otherPart:FindFirstChild("HumanoidRootPart") then
local characterWhoTouchedPart = otherPart.Parent
characterWhoTouchedPart:SetAttribute("IsHiding", true) -- sets the "hiding" attribute to true
-- run the hiding animation, move the player into the wardrope and so on...
end
end)
-- You will also need to make a "StopHiding" function which will also have to run on the Server, make sure to include this at the start of this function:
characterWhoTouchedPart:SetAttribute("IsHiding", false)
Why your code doesn’t work…
due to you wrapping the wardropePart.Touched event inside of the rushPart.Touched event it means that the inWardrope value doesn’t change to true before checking again and leading to you getting killed.
NOTE:
you will need to fill in the blanks of the code above, I only wrote the basic plan for you to build on
if you have any other questions or errors let me know!
Kills me instantly because I didn’t have time to go into a wardrobe. I want to make it so that I have 5 seconds before the script checks if I’m hiding, and if not, kills me.
Oh my bad, here is an amended version, but what ruckuhs is saying is also quite right, this method of working is fairly crude.
rushPart.Touched:Connect(function(touch)
local playerTouching = playerService:GetPlayerFromCharacter(touch.Parent)
if playerTouching then
if playerTouching == player then
local inWardrobe = false
spawn(function()
for i = 1, 6, 1 do
light.PointLight.Brightness = 0
task.wait(0.05)
light.PointLight.Brightness = 0.38
task.wait(0.05)
end
task.wait(5)
if inWardrobe == false then
player.Character.Humanoid.Health = 0
end
light.PointLight.Brightness = 0
end)
wardrobePart.Touched:Connect(function(wardrobeTouch)
local playerWardrobeTouching = playerService:GetPlayerFromCharacter(wardrobeTouch.Parent)
if playerWardrobeTouching then
if playerWardrobeTouching == player then
inWardrobe = true
end
end
end)
end
end
end)