I am trying to make a part that if nobody is touching it, it teleports a random player to another part.
this is the code:
local parent = script.Parent
local parts = parent:GetPartsInPart() -- get all the parts that are touching the part
local players = game:GetService("Players")
function Touching(part) -- function
if part.Parent:FindFirstChild("Humanoid") then -- check to see if the part that left has Humanoid as a sibling
for i, parts in pairs() do -- put the parts variable into a for loop
if i.Parent:FindFirstChild("Humanoid") then --double check if the part is part of the character
if i == nil then -- check to see if there are still characters touching the part
local randomPlayer = math.random(#players) -- get a random player
local randomCharacter = randomPlayer.Character or randomPlayer.CharacterAdded:Wait() --get the character of the random player
randomCharacter.HumanoidRootPart.CFrame = game.Workspace.ButtonArea -- teleport the player to the ciontrol room (aka button area)
end
end
end
end
end
parent.TouchEnded:Connect(Touching) -- make the function start when a part stops touching the part
Touching() -- make the function start when the game starts
this is the error:
GetPartsInPart is not a valid member of Part âWorkspace.MurdererManagerâ
edit: incase you are wondering what "ButtonArea " is and what âMurdererManagerâ is, this is just the type of game my game is, there are murderers that press buttons which activate traps. this script is supposed to make it so there is always someone in there.
2 Likes
Well your answer is right in the error, GetPartsInPart is not a child of murdermanager, Are you sure itâs in the right spot? If so is it getting deleted possibly? Maybe send a pic of the hierarchy?
1 Like
The only thing that is in murderermanager is the script. the murderermanager is in the workspace, and there is no way a script is deleting it because I created the part right before i made the script and it for sure is anchored.
1 Like
Wait I just realized GetPartsInPart is a function, sorry. I donât think that thatâs a usable function, Iâm pretty sure itâs :GetTouchingParts()
1 Like
Iâll try this, hopefully it works
1 Like
now i have this: Workspace.MurdererMannager.Script:6: attempt to index nil with âParentâ - Server - Script:6
17:08:14.682 Stack Begin - Studio
17:08:14.682 Script âWorkspace.MurdererMannager.Scriptâ, Line 6 - function Touching - Studio - Script:6
17:08:14.682 Script âWorkspace.MurdererMannager.Scriptâ, Line 21 - Studio - Script:21
17:08:14.682 Stack End - Studio
I think there is a much bigger error with this script
1 Like
Oof, ok try putting an instance inside of getpartsinpart()
1 Like
Arenât instances used to create things?
1 Like
I have been scripting for 2 years but I donât think Iâm very good at it
starting from knowing nothing
1 Like
Thatâs alright, Itâs taken me a couple years too, Itâs a lot to memorize. I have an idea, go back to GetTouchingParts(), I think those other errors were from different mistakes in your code. If you could take a screenshot of those errors that would be great.
1 Like
This is the screenshot:
same error that a told you
2 Likes
I donât think the line 21 thing means much, that is just the call for the function pretty much saying that the error is in the function
1 Like
Ok So âpartâ on line 6 is nil, maybe before line 6 add a print() and print part to see if itâs nil, you could also use warn which does the same thing but itâs brighter and easier to read.
1 Like
Oh I think I know what the problem is
1 Like
When I join the game, it does not spawn me on the part. no part has actually stopped touching the part, and no part actually started touching it making it so that there is no part there and the function which calls it when the game starts is actually just messing up the code. I pretty much need to remove line 21
2 Likes
Ok so that did fix the error but there is another one. Are you willing to help me? (you donât have too)
if you do want to help, this is the error:
17:28:57.647 Workspace.MurdererMannager.Script:7: missing argument #1 to âpairsâ (table expected) - Server - Script:7
17:28:57.647 Stack Begin - Studio
17:28:57.647 Script âWorkspace.MurdererMannager.Scriptâ, Line 7 - function Touching - Studio - Script:7
17:28:57.653 Stack End -
I think it might mean that I need to get the table of the parts variable.
1 Like
Yeah sorry! I was in the middle of something! Always happy to help! Yes
Instead of this:
for i, parts in pairs() do
You need to do this:
for i, v in pairs(parts) do -- v can be whatever you want it to be as it's a variable
2 Likes
Do you know what the i, and the v is? Just wondering.
1 Like
thanks, and sorry for the late response.
yes, I do know what i and v are I just got them mixed up though
1 Like
That got rid of the error, but it still does not work.