.Touched Event not working on Union with Remote Event

Hello, i am trying to make a collectible that increases the value of a GUI so it shows the amount of collectibles you have.
I’m also trying to make another one which does the same thing as the first one, only this time it increases a different value.

I have made a Remote Event in Replicated Storage so i could get the PlayerGui from a local script to a server script.

Collectible 1 :

local Jiggy = script.Parent
local Music = script.JiggyMusic
local Light = Jiggy.Light
local Dance = script.JiggyDance
local Sparkles = Jiggy.Sparkles
local ServerStorage = game:GetService("ServerStorage")
local JTool = ServerStorage.JiggyTool
local PassPlayer = game:GetService("ReplicatedStorage").PassPlayer

function onTouched(Hit)
	if Hit and Hit.Parent:FindFirstChild("Humanoid") then
		PassPlayer.OnServerEvent:Connect(function(Player, PlayerGui)
			local BKGui = PlayerGui.BKGui
			local JImg = BKGui.JiggyImg
			local JAmt = JImg.JiggyAmt
			local Val = JAmt.Jiggies
			local TS = game:GetService("TweenService")
			local TI1 = TweenInfo.new(1, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out)
			local TI2 = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
			local T1 = TS:Create(JImg, TI1, {Position = UDim2.new(0.449, 0, 0.205, 0)})
			local T2 = TS:Create(JImg, TI2, {Position = UDim2.new(0.448, 0,-0.192, 0)})
			Val.Value = Val.Value + 1
			Sparkles.Enabled = false
			Jiggy.CanTouch = false
			Jiggy.Transparency = 1
			Light.Enabled = false
			local Humanoid = Hit.Parent:WaitForChild("Humanoid")
			local Root = Hit.Parent:WaitForChild("HumanoidRootPart")
			for i,v in pairs(Humanoid:GetPlayingAnimationTracks()) do
				v:Stop()
			end
			Root.Anchored = true
			JTool.Parent = Humanoid.Parent
			local DanceAnim = Humanoid:LoadAnimation(Dance)
			JImg.Visible = true
			T1:Play()
			Music:Play()
			DanceAnim:Play()
			wait(3)
			Root.Anchored = false
			Music:Stop()
			JTool.Parent = ServerStorage
			T2:Play()
			-------------------
			T2.Completed:Wait()
			-------------------
			JImg.Visible = false
			Jiggy:Destroy()
		end)
	end
end

Jiggy.Touched:Connect(onTouched)


Collectible 2:

local NoteMusic = script.NoteMusic
local Sparkles = Note.Sparkles
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PassPlayer = ReplicatedStorage.PassPlayer

function onTouched(Hit)
	if Hit and Hit.Parent:FindFirstChild("Humanoid") then
		PassPlayer.OnServerEvent:Connect(function(Player, PlayerGui)
			local BKGui = PlayerGui.BKGui
			local NoteImg = BKGui.NoteImg
			local NoteAmt = NoteImg.NoteAmt
			local Notes = NoteAmt.Notes
			Notes.Value = Notes.Value + 1
			NoteMusic:Play()
			Note.CanTouch = false
			Note.Transparency = 1
			Sparkles.Enabled = true
			wait(1)
			Sparkles.Enabled = false
			Note:Destroy()
		end)
	end
end

Note.Touched:Connect(onTouched)

The 1st script is supposed to show the GUI, the amount of “Jiggies” you have, play a player animation and play a music. However, when i touch it, nothing happens. I swear this worked before but all of a sudden it just doesn’t work.
The 2nd script is supposed to show the amount of “Notes” you have (on the GUI), play a sound and show some sparkles.

Keep in mind, these 2 scripts are Server Scripts inside the Unions they belong to. Here’s the local script:

llocal Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerGUI = Player.PlayerGui
local Event = game:GetService("ReplicatedStorage").PassPlayer

Event:FireServer(PlayerGUI)

Please help me with this. It happened all of a sudden, before it worked completely fine, now it doesn’t.

I would like to assume that the local script is not inside the model itself. If it is, then that is the problem. Local scripts can only run in PlayerGui, Backpack, PlayerScripts, and the player’s character.

Now, I cannot tell you the exact issue here, but I notice something that will be a huge issue in the future. You are putting event listeners inside another event listener that will get fired multiple times. What do I mean? You have the OnServerEvent inside the Touched event. Every time the collectible is touched, it will create yet another OnServerEvent listener. This, in turn, will cause many problems because if the player touches the part multiple times, whatever is in the OnServerEvent function will run more times than expected. Although it looks like the note will get deleted after one touch, keep in mind that you cannot trust that the OnServerEvent will fire instantly, so there’s a chance that two touched events will fire before the OnServerEvent actually fires, and in your cause, this would cause your Notes value to be bigger than expected.

My suggestion for debugging your problem is adding print statements to your code to see what runs and what doesn’t. I’d add one right before the FireServer call in the local script, and on the server scripts, I’d add it when the onTouched function begins, when the OnServerEvent function begins, and in-between the code.

Hey, so the local script is in StarterGui, and it does print "Event Fired", however, it doesn’t print the message which is in the OnServerEvent function, and it prints the message right between the function onTouched()
line and the if Hit and Hit.Parent:FindFirstChild("Humanoid") then line.

Fixed it by moving the PassPlayer.OnServerEvent line before the function onTouched(Hit) line and adding local to the function onTouched() line.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.