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

Scripts from models cloned from ReplicatedStorage into Workspace (client-sided) don't work?

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

2 comments

0

Are you cloning this within a local script?

0

Yes, to reiterate, the cloning happens from ReplicatedFirst, calling ReplicatedStorage to clone to Workspace. All this happening from the client.

1 answer

✓ Accepted answer

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)

3 comments

0

While this would work if trees were the only objects spawned in the world, I also have flowers and yet to implement objects that will be cloned the same way (reason why clone is called clone and not tree). Though I could check if the clone is called tree and connect an event based on that? I would use a server script if it would only be used for the event. Using it to spawn trees is unideal though

0

Edited the post to try to fit the requirement ^^

0

In the few tests that I made, this works wonderfully, thank you so much! :D The real test is getting this integrated with multiplayer, but remote events could fix that, else I could make it single player. Thank you again!! :D

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