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

Touched function for my game?

So. I'm making this for a bank robbery, you enter, touch the invisible part and get a wanted level. Here is my script:

local crime = require(game.ServerScriptService.Crime)
local trigger = script.Parent

trigger.Touched:Connect(function(hit)
	if hit then
		local char = hit:FindFirstAncestorWhichIsA("Model")
		crime.increaseWantedLevel(char, 1)
	end
end)

I'm not sure what's wrong, I even put print("hit") so it prints hit when I touch it, but it doesn't work. How can I fix this?

1 comment

0

I'm guessing the invisible part is unanchored while having no colisions making it go thru the map and getting destroyed? I don't see why the touched connection would fail and unless you're getting any errors that seems like the most reasonable thing.

1 answer

I think you should check if the hit.Parent has a humanoid so it checks if a character touches it instead of a random part, like this:

 local crime = require(game.ServerScriptService.Crime)
 local trigger = script.Parent
 
 trigger.Touched:Connect(function(hit)
    local humanoidCheck = hit.Parent:FindFirstChild("Humanoid")
      if humanoidCheck then
 	local char = hit.Parent
 	crime.increaseWantedLevel(char,1)
      end
end)

Since only characters have humanoids, this makes sure the touch event only runs when a character touches it, not when a random part touches it.

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