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

My Script won't work after I cloned it into a part what's wrong?

So... the title may be not good but I had nothing else in Mind:

I have tried to make a script that copies another script and a ClickDetector inside of it into a part... Called... Part. Everything worked fine. Inside the cloned script is a GUI which should appear at the Players screen if they click on the Part. The problem is: Nothing happens. I don't even get an Error or anything else it just won't work... here is my script:

script.Parent.ClickDetector.MouseClick(function()
	script.Parent.Name = "tmp"
	local p = game.Players.LocalPlayer.Name
	local pm = game.Workspace:FindFirstChild(p)
	p.Anchored = true
	local pd = script.ScreenGui:clone()
	pd.Parent = game.Players.LocalPlayer.PlayerGui
	
	pd.Enabled = true
end)

I tried it with a LocalScript and a normal script but both unsuccessfully

I also tried creating a function and call that function when the part gets clicked

function onClick()
-- Just if you don't know what I mean
end

script.Parent.ClickDetector.MouseClick(onClick)

I hope you can help!

Greetings! :D

1 comment

0

Why are you anchoring the player's name on line 5?

1 answer

You're getting the LocalPlayer so I'm assuming you're doing this in a local script. The issue is that you need to have the local script under the Player and correctly define the part getting clicked when using a ClickDetector.

ClickDetectors Wiki: Since LocalScripts only run if they are descendants of a Player or Player’s Character, it’s not a good idea to put LocalScripts inside ClickDetectors.

The above is the same if your local script was under the part being clicked. Note that you are doing this all on the client so none of this is replicated or seen by the server.

--Remember that we need to wait for instances in local scripts
local part = workspace:WaitForChild("Part") --This is the part with the ClickDetector

local cd = part:WaitForChild("ClickDetector")

--Function to be connected and with the player that clicked as parameter
function onMouseClick(player)
	local gui = script.ScreenGui:Clone()
	gui.Parent = player.PlayerGui

	--rest of your code
end

cd.MouseClick:Connect(onMouseClick)

The MouseClick event has a parameter which is the player that clicked the ClickDetector. You could use this instead of getting the LocalPlayer. Again, this code should be in a local script under the Player instance. StarterPlayerScripts is where you should put it.

You also have other things inside the MouseClick function that you shouldn't do like Anchoring the player and getting the character in workspace using FindFirstChild.

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