Help me please! I want it to print; "A cookie!"
COMMUNITY QUESTION
how to print something when someone touches a part?
2 answers
Hello! Here is a script!
wait()
function onTouched() --Making a function and giving it a name
wait() --Adding this so it doesn't print "A cookie!" right as you join it
print("A cookie!") --Printing "A cookie!"
end
script.Parent.Touched:Connect(onTouched) --Calling the function and event.
Hope this helped you!
If this helped don't forget to accept the answer! :)
As you probably know by now, baseparts have a built in Touch function that has the part that touched it as an argument and lua has a print function so basically you can do this:
script.Parent.Touched:Connect(function(hit)
print("A Cookie",hit)
end)
But we can spice up this touched function with a bit extra
for one we could have it only work when a player touches it with the built in :GetPlayerFromCharacter() function.
script.Parent.Touched:Connect(function(hit)
if hit and hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then
print("A Cookie")
end
end)
But this function has some flaws, like printing "A Cookie" every time a body part touches the parts, so we can either use a debounce or a whitelist
--[[
Option 1:
Debounce
]]
local debounce = false
script.Parent.Touched:Connect(function(hit)
if hit and hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then
if not debounce then
debounce = true
print("A Cookie")
wait(2)
debounce = false
end
end
end)
--[[
Option 2:
Whitelist
]]
local hithums = {}
script.Parent.Touched:Connect(function(hit)
if hit and hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then
if hit.Parent.Humanoid and not hithums[hit.Parent.Humanoid] then
print("A Cookie")
hithums[hit.Parent.Humanoid] = true
wait(2)
hithums[hit.Parent.Humanoid] = false--nil works also
end
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
HELP