Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I set Vector2 size to slowly decrease over time after being .Touched, using .TouchEnded?

Im using .Touched and .TouchEnded events to make "Capture Zones". I'm trying to make the Vector2 X plane to slowly shrink in size after being not touched. I know that {0,1-1} in line 28 may be wrong, I just forgot how to reference the Size it was before. I may even be completely wrong in general.

local captured = false
captureZone = game.Workspace["Location A"].CaptureZone
location = game.Workspace["Location A"]

game.Players.PlayerAdded:Connect(function(player)
	if player.Team == game.Teams.viking or game.Teams["viking chieftan"] or game.Teams["viking king"] then
		if captured ~= true then do
			script.Parent.Parent.CaptureZone.Touched:Connect(function()
				captureZone.BrickColor = BrickColor.new("Bright red")
				wait(1)
				captureZone.BrickColor = BrickColor.new("Medium stone grey")
				wait(1)
					if location.A.BillboardFrame.Viking.Size ~= Vector2({0,200},{0,50}) then
						wait(1)
						location.A.BillboardFrame.Viking.Size = Vector2({0,1+2},{0,50})	
							if location.A.BillboardFrame.Viking.Size == Vector2({0,200},{0,50})	then
							captured = true
							end
					end	
			end)
		end
	end
end)

script.Parent.TouchEnded:Connect(function()
	if location.A.BillboardFrame.Viking.Size ~= Vector2({0,1},{0,50}) then do
	wait(1.5)
	location.A.BillboardFrame.Viking.Size = Vector2({0,1-1},{0,50})
	end
end	

I'm also getting an error at "end)": Workspace.Location.A.CaptureZone.CaptureScript:5:')' expected (to close '(' at line 23) near 'end'. I'm Confused. It doesn't change color either. If anyone can improve this please help lol.

1 answer

✓ Accepted answer

Improvements

Use WaitForChild() for any instance which may not have loaded before the script

Use GetService() to retrieve the Players service

Use workspace instead of game.Workspace

You can get the current Size Offset by using the variable's Size.X.Offset Property

You dont require brackets when setting a new UDim2

Check for the player when the "ncz" is touched instead of connecting it to the player when they join the game since if they are not on the viking team when the game starts, the player may not activate the event

Issues

The size of a guiobject is determined by a UDim2 property, not Vector2

When comparing size to a specific UDim2, you need to use a .new() constructor

Your final "end" required an ending parenthesis for the connection(function()

You do not need "do" to make an if then statement

Revised Server Script

local teams = game:GetService("Teams")
local team1 = teams:WaitForChild("viking")
local team2 = teams:WaitForChild("viking chieftan")
local team3 = teams:WaitForChild("viking king")

local parent = script.Parent
local ncz = parent.Parent:WaitForChild("CaptureZone")
local captureZone = workspace:WaitForChild("Location A"):WaitForChild("CaptureZone")
local location = workspace:WaitForChild("Location A")
local frame = location:WaitForChild("A"):WaitForChild("BillboardFrame"):WaitForChild("Viking")

local captured = false

ncz.Touched:Connect(function(hit)
	if hit and hit.Parent then
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		if player and (player.Team == team1 or player.Team == team2 or player.Team == team3) and captured == false then
			captureZone.BrickColor = BrickColor.new("Bright red")
			wait(1)
			captureZone.BrickColor = BrickColor.new("Medium stone grey")
			wait(1)
			if frame.Size ~= UDim2.new(0, 200, 0, 50) then
				wait(1)
				local change = frame.Size.X.Offset + 2
				frame.Size = UDim2.new(0, change, 0, 50) 
				if frame.Size == UDim2.new(0, 200, 0, 50) then
					captured = true
				end
			end
		end 
	end
end)

parent.TouchEnded:Connect(function(hit)
	if frame.Size ~= UDim2.new(0, 1, 0, 50) then
		wait(1.5)
		local change = frame.Size.X.Offset - 1
		frame.Size = UDim2.new(0, change, 0, 50)
	end
end)

Note: if the teams are not pre-created when the game starts, then change the "team1" in the player.Team == team1 to teams:WaitForChild("viking")

2 comments

0

I'm not the greatest scripter, so Is there any way I could make it to where if a player is inside the part, the .Touch continues to run?

0

You can try Region3 and check that parts of the player in the designated region

This discussion is preserved as it was. New answers, comments, and votes are closed.