Script works in different place file but not in main game?

So yesterday i tried switching my scripts to collectionservice so that i could update them in one script and not multiple and i realized that this didn’t work, i looked for help but found really nothing so i made a devforum post (Tried to switch to collection service to be able to update scripts better yet doesn't work) and there i found out that the script works in a different place file but not in my game? I really don’t understand anything and i’m just confused, so if anyone could help i would appreciate it alot.

1 Like

Collection Service can’t be used with “Child” types. I never have had it work ever in my year and a half of coding. If I were you I would re-write this.

local CS = game:GetService("CollectionService")
for i, part in pairs(CS:GetTagged("Door")) do
	print("it works") -- prints this
	part:FindFirstChildOfClass("Attachment"):FindFirstChild("ProximityPrompt").Triggered:Connect(function(player)
		print("it triggered") -- doesn't print this
		local prompt = part.Attachment.ProximityPrompt
		local dooropen = part.DoorOpen
		local ts = game:GetService("TweenService")
		local sound = part.Sound

		local char = player.Character
		local hum = char:FindFirstChildOfClass("Humanoid")
		if hum.Health < 1 then return end
		prompt.MaxActivationDistance = 0
		game.ReplicatedStorage.Door_Open:Fire()
		local door_anim = ts:Create(part,TweenInfo.new(2,Enum.EasingStyle.Circular,Enum.EasingDirection.Out),{CFrame = dooropen.CFrame})
		door_anim:Play()
		sound:Play()
		task.wait(2)
		script.Enabled = true
	end)
end

To something like this

local CS = game:GetService("CollectionService")
for i, part in pairs(CS:GetTagged("Door")) do
	print("it works") -- prints this
	part.Attachment.ProximityPrompt:Connect(function(player)
		print("it triggered") -- doesn't print this
		local prompt = part.Attachment.ProximityPrompt
		local ts = game:GetService("TweenService")
		local sound = part.Sound

		local char = player.Character
		local hum = char:FindFirstChildOfClass("Humanoid")
		if hum.Health < 1 then return end
		prompt.MaxActivationDistance = 0 --what are you trying to do?
		game.ReplicatedStorage.Door_Open:Fire()
		local door_anim = ts:Create(part,TweenInfo.new(2,Enum.EasingStyle.Circular,Enum.EasingDirection.Out),{CFrame = dooropen.CFrame})
		door_anim:Play()
		sound:Play()
	end)
end

I remove quite a few useless things as I had no idea what they were actually doing. I also put a comment on the prompt max activation distance? Why set it to 0? Shouldn’t they be allowed to close the door? You should have a value that turns the proximity prompt off, or disables it completely if that is your goal.

Still doesn’t work.
Also sorry if the script was messy i legit just copied a tutorials script and forget to rewrite it.

It works for me in my studio, my guess is that you have something wrong in your explorer. Or you are using the cloud create, in which things are loading before CS gets them and runs.

Add task.wait(3) at the top of your script. Would be surpised at how many things it would fix if you have cloud create turned on.

What’s a cloud create?

(i need the message to be atleast 30 letters)

1 Like

I tried adding a task.wait(3) and it still doesn’t work. I’m starting to suspect that somethings just wrong with my game because it also works in a different place file

1 Like

Show me where you added task.wait(3) Also, if you don’t know what cloud create is, it enables it by default, so you are probably using it lol.

task.wait(3)
local CS = game:GetService(“CollectionService”)
for i, part in pairs(CS:GetTagged(“Door”)) do
print(“it works”) – prints this
part.Attachment.ProximityPrompt.Triggered:Connect(function(player)
print(“it triggered”) – doesn’t print this
local prompt = part.Attachment.ProximityPrompt
local ts = game:GetService(“TweenService”)
local sound = part.Sound
local dooropen = part.DoorOpen
local char = player.Character
local hum = char:FindFirstChildOfClass(“Humanoid”)
if hum.Health < 1 then return end
prompt.MaxActivationDistance = 0
game.ReplicatedStorage.Door_Open:Fire()
local door_anim = ts:Create(part,TweenInfo.new(2,Enum.EasingStyle.Circular,Enum.EasingDirection.Out),{CFrame = dooropen.CFrame})
door_anim:Play()
sound:Play()
end)
end

Something is deeper than that, I don’t think you named something correctly, or messed up where something is in your explorer. I cannot fix it without first being able to see your test world. The best way to test if it is your game is to make a new game and only add those models/scripts. Like I said, it works fine for me… I made a rig and gave it a attachment and proximityprompt. Another thing is all the random variables you had, you didn’t even know what they did… That is not good.