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

object change size on touch with an other object?

ok i just wanted to make a fireball when it touch an object to change it's size and become big (smoothly) and while it is becoming big if it touch a player the player will take damage. when it reach the size that i want the ball need to disappear but the problem is that with the script that i made the ball start becoming big and when it touch other object the script runs again so the ball never stop until it can't touch anything else here is my script

Fire.Fireball.Touched:Connect(function(touch)
	local player = touch.Parent
	if touch:IsDescendantOf(character) then return end
	if not touched then -- i made a variable at the start of the script >  local touched = false
		local touched = true
		Fire.Fireball.Anchored = true
		velocity:Destroy() -- i used this to destroy the velocity because before this script start i wanted to ball to move
		for i = 1 , 300 do
			wait(0.001)
			Fire.Fireball.Size = Fire.Fireball.Size + Vector3.new(1,1,1)
			Fire.Fireball.other1.Size = Vector3.new(Fire.Fireball.Size.x + Fire.Fireball.Size.x*0.06 ,Fire.Fireball.Size.y + Fire.Fireball.Size.y*0.06,Fire.Fireball.Size.z + Fire.Fireball.Size.z*0.06)
			Fire.Fireball.other2.Size = Vector3.new(Fire.Fireball.Size.x + Fire.Fireball.Size.x*0.06 ,Fire.Fireball.Size.y + Fire.Fireball.Size.y*0.06,Fire.Fireball.Size.z + Fire.Fireball.Size.z*0.06)
		end
		Fire:Destroy()
	end
	
	
	if touch.Parent:FindFirstChild("Humanoid") then
		touch.Parent.Humanoid:TakeDamage(30)
	end
end)

1 answer

✓ Accepted answer

I noticed that you're using a variable called touched to determine if the ball should increase in size. However, you're redefining the value in another scope rather than updating the value, so it's not working how you'd expect. Here's my attempt at an illustration:

local touched = true

do
	local touched = false
	print(touched) --> prints false
end

print(touched) --> prints true

Only use the local keyword if you're defining the variable for the first time.

local touched = true

if true then
	touched = false -- notice the excluded local keyword
	print(touched) --> prints false
end

print(touched) --> false

You can read more about scopes here

Anyways, you ought to disconnect the touched event rather than using a variable to disregard further calls.

local fireBallTouched

fireBallTouched = Fire.Fireball.Touched:Connect(function(touch)
	local player = touch.Parent
	if touch:IsDescendantOf(character) then return end

	-- disconnect to no longer receive calls for this connection
	fireBallTouched:Disconnect()

	-- etc.
end)

You can read more about RBXScriptConnection use and events here.

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