Problem with model ontouch

Hi DEVs!

Recently i had problem with this script, becouse its two meshparts, not one i and wasnt able to use it, i was rescripting it 2x times but i still dont know how to make this work on model, instead of part/meshpart

So what i want to achieve?
I want to make this script work on model

db = false
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if db == false then
			db = true
			script.Parent.Transparency = 1
			script.Parent.CanCollide = false
			player.leaderstats.Donuts.Value = player.leaderstats.Donuts.Value + 5 
			wait (5) 
			script.Parent.Transparency = 0
			script.Parent.CanCollide = true
			db = false
		end
	end
end)	

Thanks for your time
Krrtek

1 Like

You can’t do a touch event on a model, It has to be a part

1 Like
db = false
local Model = workspace.YourModel

for _, Part in pairs(Model:GetChildren()) do
if Part:IsA("MeshPart") or Part:IsA("Part") then
Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if db == false then
			db = true
			script.Parent.Transparency = 1
			script.Parent.CanCollide = false
			player.leaderstats.Donuts.Value = player.leaderstats.Donuts.Value + 5 
			wait (5) 
			script.Parent.Transparency = 0
			script.Parent.CanCollide = true
			db = false
		end
	end
end)
end
end	

And i will put this script into the model?

Depends on you, you can put it wherever you want but you need to set the path to the model.

local Model = workspace.Model -- change this

and you can put the script for example to ServerScriptService.

1 Like
local db = false
local model = script.Parent -- Where your model is

for _, v in pairs(model:GetDescendants()) do
   if v:IsA('BasePart') then
     v.Touched:Connect(function(Hit)
         if Hit.Parent:FindFirstChild('Humanoid') and not db then
              db = true
              local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
              if not player then return end
			script.Parent.Transparency = 1
			script.Parent.CanCollide = false
			player.leaderstats.Donuts.Value += 5 
			wait (5) 
			script.Parent.Transparency = 0
			script.Parent.CanCollide = true
			db = false
         end
      end)
  end
end
1 Like

Models don’t have a .Tocuhed event, you’ll need to loop through every object inside the model and set up a connection.

Here’s an example:

local Model = ModelLocation
for _, Object in ipairs(Model:GetChildren()) do -- assuming everything inside has a .Touched event.
   Object.Touched:Connect(function(hit)
      -- code.
   end)
end
1 Like

:joy: :joy: :joy:

-- instead of
Part:IsA("MeshPart") or Part:IsA("Part")

-- pro gamer move
Part:IsA("BasePart")
3 Likes

and thats what im calling progamer move