I set up a system where when a player touches Trash1
, it disappears, and a new one spawns. However, the newly spawned Trash1
doesn’t disappear when touched because it doesn’t have the script inside it. Also, the new Trash1 doesn’t keep the texture/image I originally applied.
Additionally, Trash1 is a mesh part that I obtained from the internet. I tried using the built in AI but I think it made it worse.
This script is in trash1 so it affect stinkpoints(a currency shown in leaderboard:
local object = script.Parent
object.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
end
local stinkPoints = leaderstats:FindFirstChild("StinkPoints")
if not stinkPoints then
stinkPoints = Instance.new("IntValue")
stinkPoints.Name = "StinkPoints"
stinkPoints.Value = 0
stinkPoints.Parent = leaderstats
end
stinkPoints.Value = stinkPoints.Value + 5
object:Destroy()
end
end)
This is in serverscriptservice and it tells where to spawn:
local object = game.Workspace.Trash1
function getRandomPosition()
local minX, maxX = -1248, -1244
local minZ, maxZ = -50, -46
local yHeight = -101.361
return Vector3.new(math.random(minX, maxX), yHeight, math.random(minZ, maxZ))
end
function spawnObject()
local newObject = object:Clone()
newObject.Parent = workspace
-- Position the object within the defined range
newObject.Position = getRandomPosition()
-- Despawn after 15 seconds
task.delay(15, function()
if newObject then
newObject:Destroy()
end
end)
end
while true do
task.wait(5) -- Adjust spawn frequency
spawnObject()
end
Sorry if its hard to understand!