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

How to make it so that when I touch it, the function triggers?

So I'm making a CTF game and here's what I have so far

script.Parent.RedFlag.Handle.Touched:connect(function()
for _, player in pairs(game.Workspace:GetChildren()) do
    if player:FindFirstChild('RedFlag')then
        print(player.Name..' has the RedFlag!')
   	end
end
end)

But the problem is that when I play the game in studio, the output says the parent that its in at the start, not when I touch it. So how do I make it so that when I touch it, the function triggers?

1 answer

✓ Accepted answer
script.Parent.RedFlag.Handle.Touched:Connect(function(char) --the character that touched
	if char ~= nil then --make sure it exists
		local player = game.Players:GetPlayerFromCharacter(char.Parent) --get the player from the character
		if player ~= nil then --if there is a player
			print(player)
			if char.Parent:FindFirstChild("Humanoid") then --if they have a humanoid
				if char.Parent:FindFirstChild("Humanoid").Health ~= 0 then --if they are still alive
					print(player.Name .. " has the Red Flag!")
					--code
				end
			end
		end
	end
end)

I hope this helps! accept if it worked :)

7 comments

0

Thanks! I'll try this out

0

I just tried it out and when I touch the flag, it's not saying that I have the flag in the output. Do you know why?

0

I also tried removing player.Name.. and it still doesn't print. PS: I made "Has the Red Flag!" to 'Has the Red Flag!'

0

edited it so now it should work

0

no need to change any of it

0

Ok

0

Thank you so much!

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