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

How do you pass a variable through two functions?

I have a function that runs when a condition is met inside of another function. Here is my code:

local bullet = game.ReplicatedStorage.testbullet
local head = script.Parent:FindFirstChild("Head")
local ReplicatedStorage = script.Parent
local event = Instance.new("RemoteEvent", ReplicatedStorage)
event.Name = "bullet"
local debounce = false
local sound = Instance.new("Sound")
sound.Parent = head
sound.SoundId = "rbxassetid://392838370"
sound.Volume = 2
sound.Playing = false
local sound2 = Instance.new("Sound")

local function Hurt(Part)
	if debounce == false then
		if Part.Parent:FindFirstChild("Humanoid") then
			Part.Parent.Humanoid.Health = Part.Parent.Humanoid.Health-25
			debounce = true
			Part.BrickColor = BrickColor.new("Really red")
			Part.Material = "Neon"
			wait(1)
			Part:Destroy()
			wait(1)
			debounce = false
		end
	end
end
 
local function shootbullet()
    local debounce = false
    local Fireball = bullet:Clone()
	--local attachment = Instance.new("Attachment")
	--attachment.Parent = script.Parent.Parent:FindFirstChild("Left Arm")
	--local particles = game.ReplicatedStorage["Cyborg Assets"].particles.Attachment.ParticleEmitter:Clone()
	--particles.Parent = attachment
	--sound.Playing = true
	wait(0.7)
    Fireball.BrickColor = BrickColor.new("Institutional white")
    Fireball.Transparency = 0
    Fireball.TopSurface = 0
    Fireball.BottomSurface = 0
    Fireball.CanCollide = false
    Fireball.CFrame = head.CFrame*CFrame.new(0,0,-10)
    Fireball.Parent = game.Workspace
    local BodyVelocity = Instance.new("BodyVelocity")
    BodyVelocity.Parent = Fireball
	BodyVelocity.maxForce = Vector3.new(math.huge, math.huge, math.huge)
	BodyVelocity.velocity = head.CFrame.lookVector*50
	Fireball.Touched:connect(Hurt)
	--attachment:Destroy()
	--particles:Destroy()
	wait(10)
	Fireball:Destroy()
end
event.OnServerEvent:connect(shootbullet)

Is there a way to send the variable "Fireball" through the "Fireball.Touched:connect(Hurt)" little snippet of code?

1 answer

You can pass a variable like this because it is already defined, you are just changing the value:

local value

clickdetector.MouseButton1Click:Connect(function() --Just an example function
	value = 1
end)

clickdetector2.MouseButton1Click:Connect(function() --Just another example function
	print(value)
end)

This will print 1 because it changes the value of the variable

5 comments

0

Also, I suggest you use :Connect instead of :connect() because lower-case is deprecated

0

and it looks worse

0

The variable is defined inside of the second function, so it is unreachable inside of the first function. I need the functions to be in this order to allow the projectile to actually do damage properly.

0

This concept should work. If you define "Value" outside the functions (at the top) and change the value from another function, it should replicate the value to all the other functions as well

0

I’ll try that.

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