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

Need to insert a script into randomly generated part?

I've tried to figure this out for ages now, and i'm going to try my luck here.

So I have a script that randomly generates parts in my map, however i need each of those parts to also contain a script that detects whenever a player touches the part.

Currently ive tried to use the :clone function to clone it into the parts, but it keeps printing an error saying that there is no part with that name, because it hasent been generated yet. Even with the wait() the same error occures.

This is my current scripts


Script for randomly generating parts

while wait() do
		`local pos1 = math.random(-345,1045)
		local pos2 = math.random(-345,1045)
		food = Instance.new("Part", game.Workspace.FoodList)
		food.Name = "Part"
		food.Shape = Enum.PartType.Part
		food.CanCollide = false
		food.Size = Vector3.new(2,2,2)
		food.BrickColor = BrickColor.Random()
		food.Position = Vector3.new(pos1, 36, pos2)
		food.TopSurface = Enum.SurfaceType.Smooth
		food.BottomSurface = Enum.SurfaceType.Smooth
		food.Anchored = true
		food.Reflectance = 0`
end

My current Script to put the script into the parts

local storage = game:GetService("ServerStorage")
local part = storage["PartTouched"]:clone()

part.Parent = game.ServerScriptService


Nothing seems to work as i can figure out.

Please help me

1 comment

0

What line does the error appear on? and what is the exact text of the output?

1 answer

✓ Accepted answer

You are cloning the script into ServerScriptService, not the part itself.

local storage = game:GetService("ServerStorage")
while wait() do
        local pos1 = math.random(-345,1045)
        local pos2 = math.random(-345,1045)
        food = Instance.new("Part", game.Workspace.FoodList)
        food.Name = "Part"
        food.Shape = Enum.PartType.Part
        food.CanCollide = false
        food.Size = Vector3.new(2,2,2)
        food.BrickColor = BrickColor.Random()
        food.Position = Vector3.new(pos1, 36, pos2)
        food.TopSurface = Enum.SurfaceType.Smooth
        food.BottomSurface = Enum.SurfaceType.Smooth
        food.Anchored = true
        food.Reflectance = 0
	local part = storage["PartTouched"]:clone()
	part.Parent = food
end

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