part.Touched:Connect(function()) not working

My mistake, it doesn’t need to be a LocalScript. That code in either a Script or LocalScript works for me.

wdym by obj? and the green bubble is the one supposed to be touching.

So, the obj is what the humanoid is touching, or what the character is touching. We then are going to be checking if that is in the game or not by saying if obj ~= nil then.

The obj could be a part, meshpart, union, etc.
obj = object

workspace:GetPartsInPart(greensphere)

If the green bubble is supposed to be being touched, then use the other script that I have provided to you.

I put it down below:

local part = script.Parent:WaitForChild("greenbubble or something")

part.Touched:Connect(function(obj)
	if obj ~= nil then
		print(obj.Name)
		return obj
	end
end)

Make sure it is any type of script inside of StarterPlayer > StarterCharacterScripts.


i tried what you said but i get this error when trying to locate the sphere from the character
image

maybe this should help

game.Players.PlayerAdded:Connect(function(plr)
	local plrName = plr.Name
	local character = workspace.Character:FindFirstChild(plrName)

	local ColSphere = character:WaitForChild("ColSphere")
	if ColSphere ~= nil then
		ColSphere.Touched:Connect(function(obj)
			if obj ~= nil then
				print("hey")
				return obj
			end
		end)
	end
end)

i think you could also add a wait() before getting the players name so the server can wait a bit and process the information


is it just me or is this a little ridicilidoncilous

should i maybe add a button that i click when the game loads to make sure everything is loaded before initiating the script?

You could try that, or you could try this serverscript.

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(chr)
		if chr ~= nil then
            print("char")
			local ColSphere = chr:WaitForChild("ColSphere")
			if ColSphere ~= nil then
				ColSphere.Touched:Connect(function(obj)
					if obj ~= nil then
						print("hey")
						return obj
					end
				end)
			end
		end
	end)
end)

I dont really know what the problem could be though.
I also made this script for the clientside, it goes into StarterPlayerScripts.

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

wait()

spawn(function()
	if character then
		print("character")
		local ColSphere = character:WaitForChild("ColSphere")
		if ColSphere ~= nil then
			ColSphere.Touched:Connect(function(obj)
				if obj ~= nil then
					print("hey")
					return obj
				end
			end)
		end
	end
end)

I used some inspiration from this article

an easy way to set this up is just tag the block with collectionservice then watch for parts that are added to this tag with a server script if you need it on the server and setup the touched connection there

this will also allow you to tag any block you want setup the same way or when any part tagged enters the workspace it will setup with that touched event

Also if there is a tag that enters the workspace before your server script runs you will need to iterate through the tagged items

here is the addedsignal for watching tags

This should work … something else is wrong. Is CanTouch checked?

2 Likes

yes… part of me thinks maybe it was because script loaded before the part?

nothing about this is easy… just added to my headache

Should come back with arm, leg, foot … all kinds of touching.
Try testing just this on a separate program.

YOU GOT IT! turns out i just had to move the part and script else to server storage instead of replicated storage for some reason. Thanks everyone i really appreciate all the help. Legends :+1:

1 Like

I put this together and recreated a green sphere, changed my character, and got it to print on touch.


The character I placed in StarterPlayer named “StarterCharacter”.
The colsphere in replicated storage being cloned, had CanCollide = false, and CanTouch = true.

--colSphere Script
script.Parent.Touched:Connect(function(hit)
	print(hit.Name)
end)
--Script to add the sphere to the character
local RS = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(model)
		print("character added")
		local colsphere = RS.ColSphere:Clone()
		local weld = Instance.new("Weld")
		weld.Part0 = model.PrimaryPart
		weld.Part1 = colsphere
		weld.Parent = model.PrimaryPart
		colsphere.Parent = model
	end)
end)

At this point I think it is most likely an issue with how or when the character is added/changed.

(Oops I was too late :P)

This looks like it is going to be a fun game, so you are very welcome.

1 Like

The way you got that set up it will touch many things and keep firing touch everytime they move a bit.
You may want to debounce that a bit …

2 Likes

it was just for testing i will clean everything up nicely, thanks again!

2 Likes

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