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

How do I connect an Event to a Function?

Below is the script I am using to tally points for my game, it's almost complete but I've run into a small problem of connecting the gottem() function to an Event listener which is watching to see if the script.Parent gets a new listener which is a Folder. Please help!

local sP = script.Parent
local amt = 0


local function gottem() -- adds three values and displays them
local descendants = sP:GetDescendants()
for index, descendant in pairs(descendants) do -- adds values
	if descendant:IsA("NumberValue") and descendant.Name == "Points" then
		amt = descendant.Value + amt
	end
end

print(amt)

local totalWins = sP:FindFirstChild("totalWins")
local totalPoints = sP:FindFirstChild("totalPoints")
local totalStars = sP:FindFirstChild("totalStars")

if totalWins then -- changes the display
	local amt = totalWins
	totalWins = sP:FindFirstChild("totalWins").Value == amt
	end
end

sP.DescendantAdded:connect(function(p)
	if p:IsA('Folder') then
		-- plz help lol
	end
end)

1 answer

Chances are, this is what you are looking for:

local function TestPrint()
	print('yo!')
end

game.Players.PlayerAdded:Connect(TestPrint)

Basically you connect it instead of doing:

local function TestPrint()
	print('yo!')
end

game.Players.PlayerAdded:Connect(function(plr)
	TestPrint()
end)

If not then sorry. If so then there are a few links you should look at: http://robloxdev.com/articles/Understanding-Functions-in-Roblox http://robloxdev.com/articles/Remote-Functions-and-Events

Hopefully, this was what you were looking for.

Best of luck developer!

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