Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Touched function for my game?

Asked by 1 year ago
Edited 1 year ago

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?

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. treeforfree123 0 — 1y

1 answer

Log in to vote
0
Answered by 1 year ago

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.

Ad

Answer this question