Local script bugging out

I’m making a game and I have a local script that isn’t picking up touched events.


for i,v in pairs(game.Players.LocalPlayer.leaderstats.BackPacks:GetChildren()) do
	local shopbutton = game.StarterGui.Templates.ItemButton:Clone()
	shopbutton.Parent = script.Parent
	shopbutton.Text = v.Name
	shopbutton.Price.Text = v.Price.Value .. "BoomBux"
	local function updateData(item)
		if item.Purchased.Value == true then
			if item.Equipted.Value == true then
				shopbutton.TextLabel.Text = "Equipped"
			else
				shopbutton.TextLabel.Text = "Equip"
			end
		end
	end


	shopbutton.MouseButton1Click:Connect(function(plr)
		shopbutton.TextLabel:TweenPosition(UDim2.new(0, 0,1, 0),Enum.EasingDirection.Out,Enum.EasingStyle.Quint,0.3)
		updateData(v)
		wait(1)
		shopbutton.TextLabel:TweenPosition(UDim2.new(0, 0,0, 0),Enum.EasingDirection.Out,Enum.EasingStyle.Quint,0.3)
	end)
	shopbutton.TextLabel.MouseButton1Click:Connect(function()
		updateData(v)
		shopbutton.TextLabel:TweenPosition(UDim2.new(0, 0,0, 0),Enum.EasingDirection.In,Enum.EasingStyle.Quint,0.3)
		local purchase = game.ReplicatedStorage.Remotes.Purchase
		local plr = game.Players.LocalPlayer
		purchase:FireServer(v)
		purchase.OnClientEvent:Connect(function()
			game.StarterGui.Sounds.Purchase:Play()
			shopbutton.BackgroundColor3 = Color3.new(0, 1, 0)
			wait(0.3)
			shopbutton.BackgroundColor3 = Color3.new(1, 1, 1)
		end)
	end)
end

game.Workspace.Packpart.Touched:Connect(function(toucher)
	print("e")
	if toucher.Parent == game.Players.LocalPlayer.Character then
		script.Parent:TweenPosition(UDim2.new(0.065, 0,0, 0),Enum.EasingDirection.InOut,Enum.EasingStyle.Elastic,0.8)
	end
end)

script.Parent.CloseShop.MouseButton1Click:Connect(function()
	script.Parent:TweenPosition(UDim2.new(0.065, 0,1.1, 0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quint,0.8)
end)

game.Workspace.Packpart.Touched:Connect(function()
	print("eeeeeeee")
end)

Here is is

I have put prints and tested every part and most of them function correctly. The only part that isnt is this

game.Workspace.Packpart.Touched:Connect(function(toucher)
	print("e")
	if toucher.Parent == game.Players.LocalPlayer.Character then
		script.Parent:TweenPosition(UDim2.new(0.065, 0,0, 0),Enum.EasingDirection.InOut,Enum.EasingStyle.Elastic,0.8)
	end
end)

script.Parent.CloseShop.MouseButton1Click:Connect(function()
	script.Parent:TweenPosition(UDim2.new(0.065, 0,1.1, 0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quint,0.8)
end)

game.Workspace.Packpart.Touched:Connect(function()
	print("eeeeeeee")
end)

This part isn’t picking up the touched event.
I’ve used this same touched event with another part from the workspace in other local script and cant find out why this one is different.
Don’t mind

game.Workspace.Packpart.Touched:Connect(function()
	print("eeeeeeee")
end)

It was to test.
I hope there is an easy fix to this problem.

LocalScripts are supposed to work on serverscripts, not LocalScripts. It’s understandable why it won’t work. Additionally, you don’t need to do this in a LocalScript, since you already know that touched returns the part that touched. If you really wanted to get the client, you’d want to fire the client.

So what you are saying is that I should use a server script that has the event in it to fire a remote to the client?

no, thats not needed. Touched events are fine on the client if you are simply dealing with UI. From the looks of it it seems you are attempting to do a .Touched event on mupltiple parts with the same name which means the script wont figure out which one you mean. To fix this you could either store all the parts in a folder and loop trhough each one with a for loop and set the .touched event accoriingly or simply rename the part. Dont forget to mark this as a solution if it helps.

I have 2 parts one for the first shop and one for the second shop, your post made me realize that I accidently used the same shop part for both shops. Thanks!

1 Like