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

Why is my script that's meant to print a message when a MeshPart touches it not working?

Hello, I've been working on this script for a few days now, but it's not working. This script is meant to print a message if a MeshPart is hitting the Part.

Here's a picture of when this is meant to happen: Here

script.Parent.Touched:Connect(function(hit)
	local h = hit
	if (h ~= nil) then
		if h:IsA("MeshPart") then
			print(h.Name.."hit me!")
			game.ReplicatedStorage:WaitForChild("vals"):WaitForChild("Winner").Value = h.Name
			script.Parent:Destroy()
		end
	end
end)

Any help would be appreciated. Thanks in advance!

2 comments

0

Why do you have parenthesis around the argument on line 3

0

idk if Lua accepts that syntax, but you could be getting an error from it which would explain why it doesn't print anything

2 answers

✓ Accepted answer
script.Parent.Touched:Connect(function(hit)
        if hit.Parent:IsA("MeshPart") then
            print(hit.Parent.Name.."hit me!") --If not working try use hit.Parent only
            game.ReplicatedStorage:WaitForChild("vals"):WaitForChild("Winner").Value = hit.Parent.Name --Here too
            script.Parent:Destroy()
        end
end)

I hope this will work

6 comments

0

OK thank you! I'll try now!

0

For some reason this doesn't work. I've done every thing you said.

0

Error code?

0

none

0

Hmm...

0

The WaitForChild can make an error like this. If the vals doesn't exists then the script waits until it appears inside the ReplicatedStorage.

Sooo, I solved this myself, because apparently if you turn Anchored on, it won't detect the part, even with CanCollide on!

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