Tween not working with Touch Event

Hello guys, I’ve been up and down with this code, I’m trying to make this part increase in size with a tween and then if while its doing this it touches another player then print said player.

I don’t know why it doesn’t print the player unless I’m basically touching it.

this script runs on the client.

local CollectionService = game:GetService("CollectionService")

local touchZone = CollectionService:GetTagged('TouchZone')

local function onTouch(tagged)
	local debounce = true
	local zone = tagged:Clone()
	local taggedsize = Vector3.new(200,200,200)
	zone.Size = Vector3.new(0,0,0)
	zone.Parent = workspace
	local Players = game:GetService("Players")
	local player = Players.LocalPlayer
	local character = player.CharacterAdded:Wait()
	local HRP = character:WaitForChild('HumanoidRootPart')

	task.spawn(function()
		while task.wait() do
			zone.Position = HRP.Position
		end
	end)

	task.spawn(function()

		while task.wait(2) do
			local TweenService = game:GetService("TweenService")

			zone.Size = Vector3.new(0,0,0)
			zone.Transparency = 0

			local zoneChanges = {
				Size = taggedsize;
				Transparency = 1
			}

			local tweenInfo = TweenInfo.new(
				1,									-- Time taken for a full animation
				Enum.EasingStyle.Linear,			-- Animation Style
				Enum.EasingDirection.InOut,			-- Animation Type
				-1,									-- Number of repeats (-1 is infinite)
				false,								-- Reverse?
				1									-- Delay between animations
			)

			local tween = TweenService:Create(zone, tweenInfo, zoneChanges)

			tween:Play()
		end

	end)
	
		zone.Touched:Connect(function(touched)
			if debounce then
				debounce = false
				local players = game:GetService("Players"):GetPlayerFromCharacter(touched:FindFirstAncestorOfClass('Model'))
				if players ~= game:GetService('Players').LocalPlayer then
					local character = players.Character
					print(character)

					-- you can index all of the player parts here using the character variable
				end

				task.wait(0.1)
				debounce = true
			end
		end)
end



for _, tagged in pairs(touchZone) do
	onTouch(tagged)
end

CollectionService:GetInstanceAddedSignal(touchZone):Connect(function(tagged)
	onTouch(tagged)
end)

First question, why are you creating lots of tween animation each 2 seconds, and all of them are infinite, so those will never stop, like stacking lots of tweens?

-- START
while task.wait(2) do -- repeating each 2 seconds

-- The -1 makes the tween infinite playing on a loop
local tweenInfo = TweenInfo.new(
				1,									-- Time taken for a full animation
				Enum.EasingStyle.Linear,			-- Animation Style
				Enum.EasingDirection.InOut,			-- Animation Type
				-1,									-- Number of repeats (-1 is infinite)
				false,								-- Reverse?
				1									-- Delay between animations
			)

local tween = TweenService:Create(zone, tweenInfo, zoneChanges) -- Creating new tween
tween:Play() -- playing it
-- REPEAT
1 Like

this isn’t the “main code”, I was testing it on another part, so I had it resetting/tweening looped to check if it was a different issue, the issue its still the same for the main code but i decided to post this one because it basically works the same

im currently cleaning the main code a bit so i can send it here

1 Like
--// Locals
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
--//

--// Paths
local Sonar = ReplicatedStorage:WaitForChild('Zone')
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local HRP = character:WaitForChild('HumanoidRootPart')
local highlight = character:WaitForChild('Highlight')
local zone = Sonar:Clone()
local light = zone:WaitForChild('PointLight')
--//

--// Values
local zoneSizeStart = Vector3.new(0,0,0)
zone.Size = zoneSizeStart
zone.Parent = workspace
light.Range = 0
local timer = 2
local zoneSize = Vector3.new(200,200,200)
local debounce = false
--//

--// Sonar Zone Following Character Position
task.spawn(function()
	while task.wait() do
		zone.Position = HRP.Position
	end
end)
--//

--// Reusable Tween
local function onTween(changes, cooldown, reverse, object)

	local tweenInfo = TweenInfo.new(
		cooldown,									-- Time taken for a full animation
		Enum.EasingStyle.Linear,			-- Animation Style
		Enum.EasingDirection.InOut,			-- Animation Type
		0,									-- Number of repeats (-1 is infinite)
		reverse,								-- Reverse?
		0									-- Delay between animations
	)

	local tween = TweenService:Create(object, tweenInfo, changes)

	tween:Play()
end
--//

--// LocalPlayer is now scanning
local function onScanning()
	if not debounce then
		debounce = true

		zone.Size = zoneSizeStart
		zone.Transparency = 0
		light.Range = 0
		light.Color = Color3.fromRGB(255, 255, 255)
		highlight.OutlineTransparency = 0

		local zoneChanges = {
			Size = zoneSize;
			Transparency = 1
		}
		onTween(zoneChanges, timer, false, zone)

		local highlightChanges = {
			OutlineTransparency = 1
		}
		onTween(highlightChanges, timer, false, highlight)

		local lightChanges = {
			Range = 160;
			Color = Color3.fromRGB(0, 0, 0)
		}
		onTween(lightChanges, timer*1.5, false, light)
		
		
		zone.Touched:Once(function(touched)
				local players = game:GetService("Players"):GetPlayerFromCharacter(touched:FindFirstAncestorOfClass('Model'))
				if players ~= game:GetService('Players').LocalPlayer then
					local character = players.Character
				print(character)
			end
		end)
		
		task.wait(timer)

		debounce = false
	end
end
--//

--// F KeyCode pressed check
local pressed = false
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.F and not pressed and not debounce then
		pressed = true
		onScanning()

		pressed = false

	end
end)
--//

the code is somewhat similar, or well at least the issue its still the same

That script is a different approach, but I think I understand what you want.

You want a part “attached” to the center of the player, that grows and when it touches other players, works as a scanner, I imagine like a “cylinder/ball” growing on repeat, like “scanning” and whenever a another player is touched by the “scanner part” you want to print it to do stuff (?)

1 Like

yeah you got it!

its basically just a scanner, its a part with a ball shape that simply grows with a tween every time the player presses F then after the debounce its set back to false it can be pressed again, all of this works on the client so its only visible to the localplayer.

here’s a video of the game for a better visual idea of what im trying to do

the player can do “pings” to basically scan nearby, but i just cant seem to make it work with Touch events :confused:

1 Like

Well, I guess the scanner system is Important for your game, and with the approach you are currently using, its easily exploitable.

When relying important mechanics for the game on clientSide, you can expect exploiters fool your system so quickly.

I suggest changing the approach.
First, handling the scanner system on server. If you wish you could still use the touchedEvent, which is just average and could fail sometimes. But, do it on server side.
Or you can check the distance of the players vs the scanner user by running a loop iterating all players in game and checking its distance to scanner user by using (Position-Position).Magnitude. All that stuff serverSided

Then, the part growing using the tween, you handle it on client, and the only purpose of that growing part its for visuals, only client effects, those wont be reading touches, or distances or anything, mere visuals.

Then, you dont need to run a spawn while loop to “attach” the part to follow the player, you would be wasting resources of client device. You could just place the growing part in the player’s character and glue it with a WeldConstraint with the HRP.

2 Likes

guess ill try this tomorrow since right now its 3AM, i did think about changing it to the server but i was 2 lazy to make the scanner only show up on the local player, guess ill have to use some remote events for that, ty ill keep this inmind to work on it tomorrow!

1 Like

Ofc its a good idea using the remotes, if the growing ball its just for visuals, and the player should press a key to activate the visuals and fire server to start reading distances, then on key pressed start the infinite tween, fireServer (server does the reading on loop), stop the loop when server decides or let the player to do it by releasing the key, stop server loop, then Cancel()/pause the tween from client.

Same for me, Im pretty lazy too, and its annoying when its about adding more remotes and listeners… :smiling_face_with_tear:

1 Like

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