Code working in one script and not the other

Title describes the issue.
I have a part that when touched should print that it was touched. (Adding function later)
I have a server script inside that part with the following code

script.Parent.Touched:Connect(function (otherPart)
print('touched')
end)

In a separate server script in a folder, I have the same code but with the correct path. The one in the part works, but not the one in the folder. Is this a roblox feature or is there a workaround. For those about to say why does it matter its because I need to access a table and don’t want to use datastores due to the rate it is accessed

A Folder is not a BasePart, .Touched is a BasePart Event that when Touched, will fire code.

I was saying the script was in a folder pointing to a part.

Its Likely not the Correct Path if it doesnt work, so both scripts please, that would be very helpful

script.Parent.Parent.FallPart.Touched:Connect(function(otherPart)
	print('TOUNEDFHDSFHIODSF')
end)

image
For the image look at FallPart and the Spots folder, the script not working is in the Spots folder.

@DasKairo Sorry forgot to reply to you

“Script.Parent” i think is the culprit here. Try to label it as workspace. whatever it is

i dont understand how do u wannt it to be ? like should the FallPart be touched and it prints somthing ??

so if a player touched it then it prints is that how u wannt it to be like ?

Yea thats what I want. Ill add the actual function later

I tried and it dident work unfortunatly.

try this.:

local fallpart = game.Workspace:FindFirstChild("FallPart")--the part

fallpart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		print(hit.Parent.Name.. " has touched it!")
	end
end)

is there any errors? make sure you have the output window open to check that

image

scripts in folders will work, contrary to what (i think (you all have broken english)) others were saying, unless they error

does the part in question have CanTouch ticked in the properties? and what is the path of the part that you specified? you could try doing :WaitForChild() instead of directly doing workspace.Part as it may not be loaded yet

Still didn’t work unfortunately.

There’s no errors just nothing happens.

1 Like

I could not replicate your error.


workspace.Folder.Script:

workspace.PrintPart.Script:

1 Like

Does the fallpart have CanQuery enabled?

Honestly I don’t understand why this is happening. Here’s the game file so you guys can see exactly what i’m seeing. Just don’t steal my game lol.
colorswitchfile.rbxl (70.2 KB)

image
^^ It never gets to this line, so it never connects the event.


Here you have a while loop that yields.

You can try doing

roundStarted = true
coroutine.wrap(startNewRound)()

So it runs the function in a different thread.
Or you can try calling the function later. Like at the end of the script.

Hi there, I checked the place out you provided (nice game!). The problem is your script is executing a cycle of logic before the Touched function is defined. All function definitions must go before an execution of logic. To fix it move this code after the Touched function definition (i.e. to the bottom of the script).

roundStarted = true
startNewRound();

You should do the game logic on the server too then people will be unable to hack the main game logic. I uploaded a version of the script with the game executed on the server and the hit logic working.

colorswitchfile_fix.rbxl (69.9 KB)

Thanks that will work! (30 minimum limit ignore)