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

If local player touches a part then do something?

Hello, I want so if a local player touches a part it does something?

I don't know how to do this.

1 answer

Ok, lets try it!

First Step : First of all we need to know how we can make a "onTouch" function. So the player can make something when it touch a specific part.

Second Step : Second, we need to know how to use a debounce so the character, player, humanoid, etc. Will only execute the determinated action 1 time per hit.

Third Step : Enjoy coding! You always have to enjoy what are you doing! Play some music while you script even play a game before so you will be relaxed.

Lets get into the code!

--> Locals <--

local damage = 25
local debounce = false

--> Code <-- 
local function onTouch(hit)
	local character = hit.Parent
	if character then
		local humanoid = character:WaitForChild("Humanoid")
		if humanoid then
			if not debounce then
				humanoid.Health = humanoid.Health - damage
			end
		end
	end
	debounce = true
	wait(6) --> The player will get only one hit so will only get 25 damage/hit
	debounce = false
end

script.Parent.Touched:Connect(onTouch)
-->Put this script into the Part

That's how we acces to a function and the character then find the humanoid and he will take damage. This can be more easy with the next line of code

humanoid:GetDamage(25)

So we can spend less time writting a simple line of code.

I hope my answer already solve your question. If it like that please accept my answer that will help me a lot.

Keep scripting!

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