Click detector event randomly stops working after player respawn but rest of the script is fine

  1. What do I want to achieve?
    A script that makes a image appear at the position of the cursor when hovering over specific tagged click detectors.

  2. What is the issue?
    Well, it seems a part of my script decides that it wont work after the player respawns, and I get no errors. I have littered the script with print statements to try and, on first join, all of the print statements are fired. But when the player dies and respawns, all of the print statements fire, except for every single one contained inside the MouseHoverEnter and MouseHoverLeave functions (basically those two functions just get completely ignored). And I have no idea why.

Server Script (ServerScriptService)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

local SetUpInteractCursor = Instance.new("RemoteEvent")
SetUpInteractCursor.Name = "SetUpInteractCursor"
SetUpInteractCursor.Parent = ReplicatedStorage

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		
		local Player = Players:GetPlayerFromCharacter(char)
		
		for _, ClickDetector in pairs(CollectionService:GetTagged("InteractCursor")) do
			SetUpInteractCursor:FireClient(Player, ClickDetector)
		end
	end)
end)

(nothing wrong with the script above, everything seems to work fine)

Local Script (StarterGUI/ScreenGUI)

local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

local SetUpInteractCursor:RemoteEvent = ReplicatedStorage:WaitForChild("SetUpInteractCursor")

local TISize = TweenInfo.new(0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)

local InteractImage = script.Parent:WaitForChild("HandCursor")

SetUpInteractCursor.OnClientEvent:Connect(function(ClickDetector)
	if UIS.KeyboardEnabled or UIS.GamepadEnabled then
		
		print("setted up "..ClickDetector.Name)
		
		local PlayerGUI = Player.PlayerGui
		local MainInteractGUI:ScreenGui

		for _, GUI in pairs(PlayerGUI:GetDescendants()) do
			if GUI:IsA("BoolValue") then
				if GUI.Name == "IsInteractCursorGUI" then
					if GUI:FindFirstAncestor("InteractCursor") then
						MainInteractGUI = GUI.Parent
						print("mainInteract Setted")
					end
				end
			end
		end

		local CursorImage = MainInteractGUI:WaitForChild("HandCursor")
		local HoverEnterAnimSize = TweenService:Create(CursorImage, TISize, {Size = UDim2.fromScale(0.027,0.05)})
		local HoverLeaveAnimSize = TweenService:Create(CursorImage, TISize, {Size = UDim2.fromScale(0.027,0)})
		local HoverEnterAnimOpacity = TweenService:Create(CursorImage, TISize, {ImageTransparency = 0})
		local HoverLeaveAnimOpacity = TweenService:Create(CursorImage, TISize, {ImageTransparency = 1})

		print("variables")

		if ClickDetector:IsA("ClickDetector") then
			
			print("clickDTCTR")
			
			ClickDetector.MouseHoverEnter:Connect(function(plr)

				print("Huvrd noplr on")

				if not plr then return end
				if plr ~= Player then return end
				
				print("huvrd")
				
				HoverEnterAnimOpacity:Play()
				HoverEnterAnimSize:Play()
				UIS.MouseIconEnabled = false

			end)

			print("there")

			ClickDetector.MouseHoverLeave:Connect(function(plr)
				
				print("Huvrd noplr off")
				
				if not plr then return end
				if plr ~= Player then return end

				print("huvrd")

				HoverLeaveAnimSize:Play()
				HoverLeaveAnimOpacity:Play()
				HoverLeaveAnimSize.Completed:Wait()
				UIS.MouseIconEnabled = true
			end)
		end
	end
end)

if UIS.KeyboardEnabled or UIS.GamepadEnabled then
	RunService.RenderStepped:Connect(function()
		local mousePosition = Vector2.new(Mouse.X, Mouse.Y)
		local udim2Position = UDim2.new(0, mousePosition.X, 0, mousePosition.Y)
		InteractImage.Position = udim2Position
	end)
end
  1. What solutions have you tried so far?
    Looking all over the internet, on the dev forum, asking chatgpt (mistake), Checking on youtube and putting the localscript in StarterCharacterScripts and StarterPlayerScripts (modifying it accordingly).

Try placing the script under StartPlayerScripts other then playerUI since player UI reset after the character resets, but the StarterPlayerScripts dont reset
image_2024-06-29_203708501

I did do that and it still does not change anything…

turns out i had to put a 5 second wait timer before the script fires to wait for all the screen guis to load. which i find ridiculous, but it works i guess,

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