Need help with local parts!

This is my first post written on DevForum and am still learning more and more about the website. I need help for something that I want to achieve and searched a long time but could not find much on local parts and how they work. I’m not at all new to Roblox studio but am new to scripting and still am trying to learn more. Basically I want too make it where the player will click on a button and a local part will spawn at a certain spot. Since that player clicked on the button I would like that player to only see the local part spawn in. For example in my game I’m working on I would like it where the player would click on a hidden button and a ladder will appear somewhere around the map.

As I said before I am very new to scripting, I normally find solutions to my problems on youtube and other posts on the DevForum but could find nearly anything on local parts and how they really work. Heres what I have so far:

Right now I have a button and inside that button is a click detector and a local script.

Screenshot (514)

This is the code that is in that local script. Again I’m not very good at scripting and need help.

local Part = game.Workspace.Camera.Part6
local Open = game.Workspace.open

function onClicked()
	Part.CFrame = CFrame.new(-173.501, 17.056, -99.776)
	Open.CFrame = CFrame.new(-164.478, -13.578, -40.731)
end 
script.Parent.ClickDetector.MouseClick:connect(onClicked)

Moving on I have a local script in StarterGui.

Screenshot (513)

Here is the code I have in that local script:

local Part = game.Lighting.Part6:Clone()

Part.Parent = game.workspace.CurrentCamera

Finally I have a part in Lighting called “Part6”

Again im just trying to make it where when a player clicks a hidden button, that button will spawn in a local part in a certain position in the game. Only the player that clicked that button can see the part. It is a lot more simple then it might sound. Thank you!

local scripts do not work inside of workspace. Use the script within StarterGui or StarterPlayerScripts.

How do you think I would do that? I was thinking on doing that before but didn’t know how to approach that.

Move the LocalScript you have in the button to somewhere like StarterPlayerScripts and change it so the function is called when the button is pressed (ex. game.Workspace.Button). If you want changes to happen on the server you should look into RemoteEvents.

You already have the script how it is needed, so you should simply move it to StarterPlayerScripts. Although, I would add a WaitForChild just in case.

local Part = game.Workspace.Camera:WaitForChild("Part6")
local Open = game.Workspace.open

function onClicked()
	Part.CFrame = CFrame.new(-173.501, 17.056, -99.776)
	Open.CFrame = CFrame.new(-164.478, -13.578, -40.731)
end 
script.Parent.ClickDetector.MouseClick:connect(onClicked)

I’m pretty sure they would as they load on the client, as long as you’re referencing the correct directories to what’s being used in the workspace.

Local scripts, as to being inside of workspace, would only work inside of a player’s character. If it is inside of anything else, it won’t run.

Nevermind, I’m pretty sure your right I got confused with something lol my bad

but yes, with in the character :+1:

Thank you but im very sorry, I dont really understand. It says that the ClickDetector is not a valid member of PlayerScripts “Players.naderbocker2.PlayerScripts”. How would I connect the ClickDetector?

If the button is in workspace, you need to do game.Workspace.Button.ClickDetector to reference the part. ClickDetector will not be a valid member of PlayerScripts.

1 Like

ClickDetector would be a member of workspace?

Like this right?

local Part = game.Workspace.Camera:WaitForChild("Part6")
local open = game.Workspace.Button
local ClickDetector = game.Workspace.Button.ClickDetector

function onClicked()
	Part.CFrame = CFrame.new(-173.501, 17.056, -99.776)
	open.CFrame = CFrame.new(-164.478, -13.578, -40.731)
end 
script.Parent.ClickDetector.MouseClick:connect(onClicked)

Oh, yes, @FindPrin provided a solution to that. Change script.Parent to workspace.Button. (forgot to change that part, my bad)

local Part = game.Workspace.Camera:WaitForChild("Part6")
local Open = game.Workspace.open

function onClicked()
	Part.CFrame = CFrame.new(-173.501, 17.056, -99.776)
	Open.CFrame = CFrame.new(-164.478, -13.578, -40.731)
end 
workspace.Button.ClickDetector.MouseClick:connect(onClicked)
1 Like

Try this code


local Part = game.Workspace.Camera:WaitForChild("Part6")
local Open = game.Workspace:WaitForChild(“Button”)

function onClicked()
	Part.CFrame = CFrame.new(-173.501, 17.056, -99.776)
	Open.CFrame = CFrame.new(-164.478, -13.578, -40.731)
end 
Open.ClickDetector.MouseClick:connect(onClicked)

1 Like

Thank you all so much! It ended up working fine so far. Ill let you know if I run into any problems and sorry for all the confusion. I guess I should keep posting more problems I run into on the DevForum, its very helpful and fast.

2 Likes