Local script doesn't run

So i have a problem when a local script just doesn’t run.
I’ve tried putting it pretty much in everywhere, where a local script can run, but still nothing.
Here’s is the problematic script

local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")
local CollectableTag = CollectionService:GetTagged("Collectable")

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid
local HumanoidRootPart = character.HumanoidRootPart

local minimum_distance = 6
local cooldown_time = 0.1
local cooldown = false

for _, collectable in pairs(CollectableTag) do
	print(collectable) -- Even this doesn't work
	collectable.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if not cooldown then
				cooldown = true

				local Info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false)
				local Params = {Position = HumanoidRootPart.Position}
				local CollectTween = TweenService:Create(collectable, Info, Params)
				CollectTween:Play()
				task.wait(0.5)
				collectable:Remove()
				print("Collected")
				local collectedItem = game:GetService("ReplicatedStorage").Collectables:FindFirstChild(collectable.Name)
				collectedItem.Parent = player.Backpack

				wait(cooldown_time)
				cooldown = false
		end
	end)
end
7 Likes

Have you added a task.wait in the beginning? It might be running before anything loads.

2 Likes

Why would you want this to run on the client? Apparently you’re parenting a Tool to your Backpack. AFAIK that wouldn’t work. This needs to be done on the server. Using Touched probably wouldn’t be a good idea either, but I need this information backed-up.

You haven’t even verified that the script isn’t being run (such as with a print call, for example). How are you able to claim that it doesn’t execute?

Another thing: you use player.Character, assuming the player has a character at all. They might have just joined the game and it hasn’t been loaded yet. This means player.Character would be nil, and your entire script would break (yey).

Some general advice: PascalCase for services / instances, camelCase for local variables (collectableTag, minimumDistance, etc.). Try to keep up a consistent variable naming style. I’d recommend you to give a read on the Roblox Lua Style guide (although one might argue it’s a bit outdated).

3 Likes

are u sure it doesnt run cuz its not outputting anything or other ?

2 Likes

I did print statement as the literal first line of the loop and also like 10 others, but that was after the post and still nothing. Please read the script before replying. Also the touched function was from the old version of the script that i accidentally copied instead of the new one.

5 Likes

could it be cuz its not finding any tag in the client

4 Likes

Nothing. There is nothing in the output.

3 Likes

This. Because the print is inside the loop, it doesn’t prove anything.

I made a 4 print statements to test if the script got past all the variables and it got. So the loop just doesn’t run

try ipairs maybe its an index problem

So you added a print(CollectibleTag) right before the for loop?

It still doesn’t work even if i use ipairs :frowning:

where is the script at the moment

i just printed out “test1”, “test2” and so on until i got to the loop

So add a print to look for exactly what you want it to find right before it and see what it prints

its the tag ur looking correct?

currently its in starter gui, but it didnt work even in starter player scripts, starter character scripts and also in starter pack.

Well thats wierd. It printed out a blank table, even tho when i check tagged instances in tag editor the clearly are the objects that im looking for.

ima test the script rq in studiio

So at the beginning of your script add a task.wait(3)seconds