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)
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
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
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