[Help]Create an instance in a character when touched a part

I need help with creating an instance in a player character when they touch a part and remove the created instance when the character stops touching the part
As for what instance it is it can be any, preferably a tag/value

I’m hardly a coder myself and still in a novice at scripting myself
Any help would be appreciated :slight_smile:

3 Likes

Although not optimal, how I would do this is the following:

  • create a touched function and check if the part is a player.
  • If it is a player, create the instance of choice.
  • Then fire a coroutine to check if the player is still touching the part by looping through the Part:GetTouchingParts() table/array
  • If no longer touching, destroy the instance found inside of the character


local ParentPart = script.Parent


--"IDK" is the name of the value

local function CheckForPlayer(Player)
	
	while true do
		
		wait()
		
		--Get all parts that are touching the part
		local TouchingParts = ParentPart:GetTouchingParts()
		local found = false
		
		--Loop through the array, and see if there is a part of a character, if there is, make found true.
		for i,part in pairs(TouchingParts) do

			if part.Parent ~= nil then
				if part.Parent.Name == Player.Name then
					found = true
				end
			end

		end
		
		
		--if there is no longer a part of the character that is touching, delete the value.
		if found == false then
			--Delete val
			
			local Value = Player:FindFirstChild("Idk")
			Value:Destroy()
			break
			
			
		end
		
	end
	
	
	
end


--Touched function. Fires everytime something touches the part
ParentPart.Touched:Connect(function(touched)
	
	--Check if part is player
	if touched.Parent ~= nil then
		if touched.Parent:FindFirstChild("Humanoid") then
			
			--Check if player alreay has the value
			
			if not touched.Parent:FindFirstChild("Idk") then
				
				--If not, Create the instance
				local Character = touched.Parent

				--Instance
				local Val = Instance.new("IntValue")
				Val.Name = "Idk"
				Val.Parent = Character
				
				--Start checking if the player is still touching the part.
				coroutine.wrap(CheckForPlayer)(Character)
			end
			

			
			
			
		end
	end
	
	
	
	
end)
1 Like

Your code seems to be kind of a weird solution in my opinion as the topic creator never said that the object touched has to be a player or contain a humanoid.

I would edit the code so you have a predefined target.
Your script will trigger everytime, the client character touches an entity.

Thanks for solving this, it seems to be working fine for me :slight_smile:
Altho i ran into a problem where it takes a little slow to create and remove the value instead of it instantaneously creates and removes the value

I thought of a possible method where you have a boolvalue and have the part change the boolvalue’s value when it’s touched and sets it to false when the character stops touching it

But hey, your method still works and it’s better than nothing, thanks :slight_smile:

local limbsTouching = table.create(game.MaxPlayers)

part.Touched:Connect(function(hit)
  local player = game.Players:GetPlayerFromCharacter(hit.Parent)
  if not player then return end
  if limbsTouching[player] then
    table.insert(limbsTouching[player], hit)
  else
    limbsTouching[player] = {hit}
    -- Create instance and parent it to hit.Parent
  end
end)

part.TouchEnded:Connect(function(hit)
  local player = game.Players:GetPlayerFromCharacter(hit.Parent)
  if not player then return end
  table.remove(limbsTouching[player], table.find(limbsTouching[player], hit))
  if #limbsTouching > 0 then return end
  hit.Parent[**add instance name here**]:Destroy()
  limbsTouching[player] = nil
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.