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

Need to insert a script into randomly generated part?

Asked by 6 years ago
Edited 6 years ago

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

0
What line does the error appear on? and what is the exact text of the output? UgOsMiLy 1074 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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
Ad

Answer this question