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

Script within a :Clone()'d model does not run, but the original runs fine. How do I fix it?

I'm a new scripter and as a way to learn Lua I'm making a game based on Plants VS Zombies where you choose a plant and place them on a plot. I have a Pea Shooter model in Replicated Storage with a detection brick that for now only simply prints something whenever it detects something colliding it. The original model works fine when in workspace, but whenever it's cloned, the detection script stops working! I've tried disabling the script then enabling the script whenever it's cloned but obviously that didn't work. I think it may be a problem with the cloning script being local.

When a plot is clicked, it clones the Pea Shooter model from Replicated Storage to Workspace. This is a local script inside the starter pack.

mouse.Button1Down:Connect(function()
	target = mouse.Target
	playerPlant = player.PlantSelected.Value --update
	print("CLICK")
	print(playerPlant)
		if target.name == "plot" and target.Taken.Value == 0 then
			if playerPlant == "Pea Shooter" then
			cloneTower(playerPlant)
				
			end
		else
			if target.Taken.Value == 1 then
				print("sorry, thats taken")	
				
				end
			end
			
	end)
	
function cloneTower (towerName)
		tower = game.ReplicatedStorage[towerName]:Clone()
		tower.Parent = workspace.Towers
		tower.PrimaryPart = tower.Part
		tower:SetPrimaryPartCFrame(target.CFrame + Vector3.new(0,3,0))
		currentCFrame = tower:GetPrimaryPartCFrame()
		tower:SetPrimaryPartCFrame(currentCFrame * CFrame.Angles(math.rad(180), math.rad(180), math.rad(70)))
		target.Taken.Value = 1
		wait(0.5)
		tower.Detection.DetectScript.Disabled = false
end


This is the detection script within the Pea Shooter model. This works fine with the original model in the workspace, but it doesn't work when cloned!!! It doesn't even run the print.

print("HI")
function onTouched(part)
	print("TOUCH")
end

script.Parent.Touched:connect(onTouched)

3 comments

0

Yep i think the problem is it is a local script

0

He said it worked on his original model.

0

I mean the script he use to spawn is local which would not work

2 answers

✓ Accepted answer

You cloned the item with a local script, meaning that it is not actually there. Use remote events to clone the peashooter, then try it again.

1 comment

0

didn't know what remote events were used for so I didn't consider this. THIS SAVED ME FROM RIPPING OUT MY OWN HAIR THANK YOU

followed @TheMinecraftSmarts advice and used remote events

I took out the clone function in the local script and instead made it fire a remote event using the plant and plot selected as arguments. Inside Server Script Service I made a new script that took what the local script fired to clone the model.

local remoteScript = game.ReplicatedStorage:WaitForChild("Remote")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local playerPlant = player.PlantSelected.Value

mouse.Button1Down:Connect(function()
	target = mouse.Target
	playerPlant = player.PlantSelected.Value --update
	print("CLICK")
	print(playerPlant)
		if target.name == "plot" and target.Taken.Value == 0 then
			if playerPlant == "Pea Shooter" then
			
			remoteScript:FireServer(playerPlant, target)
			end
		else
			if target.Taken.Value == 1 then
				print("sorry, thats taken")	
				
				end
			end
			
	end)

This is the clone script inside Server Script Service

local remoteScript = game.ReplicatedStorage:WaitForChild("Remote")

remoteScript.OnServerEvent:Connect(function(player, towerName, targetPlot)
	
	tower = game.ReplicatedStorage[towerName]:Clone()
	tower.Parent = workspace.Towers
	tower.PrimaryPart = tower.Part
	tower:SetPrimaryPartCFrame(targetPlot.CFrame + Vector3.new(0,3,0))
	currentCFrame = tower:GetPrimaryPartCFrame()
	tower:SetPrimaryPartCFrame(currentCFrame * CFrame.Angles(math.rad(180), math.rad(180), math.rad(70)))
	targetPlot.Taken.Value = 1
end)
	

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