I'm working on a game at the moment, but I've come across a bug that I can't seem to fix. In a LocalScript in ReplicatedFirst, I do some calls to ReplicatedStorage to clone a model (which has a Script/LocalScript inside) into Workspace. However, while the model does clone into Workspace, the Script/LocalScript inside does not seem to work. This is the specific line of code that calls to clone the object in ReplicatedFirst:
local clone = game.ReplicatedStorage.TerrainModels[modelData[1]]:Clone() -- maths stuff for position and creates folder in Workspace clone.Parent = folder -- folder in Workspace
This is the code in the Script/LocalScript in the model from ReplicatedStorage
script.Parent.Touched:Connect(function() print("Tree touched.") end)
The reason why I'm using Script and LocalScript interchangeably is because I've tried using both type of scripts to no avail.
The first block of code does clone the model (and child script), but the script itself does not print anything to the console.
In other words, I'm asking for a way to run scripts from models cloned from ReplicatedStorage on the client? Thank you :)
You can instead clone the model first, then connect an event. If you want the tree to be visible to everyone, use a server script, else use a local script. Or, if this touch event serves an important purpose, always use server script.
Edit: Did a search, and instead of using if statements, you can put functions in a table and call them according to models’ names.
local ReplicatedStorage = game:GetService('ReplicatedStorage') local terrainModelsFunctions = { ['Tree'] = function() print('Tree touched') end; ['Flower'] = function() print('Flower touched') end; } local clone = ReplicatedStorage.TerrainModels[modelData[1]]:Clone() clone.Parent = folder clone.Touched:Connect(function() terrainModelsFunctions[clone.Name]() end)