Okay so I have a model with a Script in it that clones a LocalScript into the Player's backpack when they click a ClickDetector in the model.
But in the LocalScript I want to reference the the model that they originally clicked (so I could do stuff like use :MoveTo
or focus the Camera on it).
So how can I do that? The LocalScript is inside the Player's PlayerGui but the model has no relation to the Player.
You could create an object value and give it the value of the model, then put the object value in the local script. The local script can then reference the model using the object value inside of it:
local model = script.value.Value
Example:
Script in ClickDetector
--Assuming the local script's name is "InsertScript" script.Parent.MouseClick:connect(function(player) if not player.Backpack:FindFirstChild("InsertScript") then--you can remove this if you want duplicates. This specific script works better with duplicates local clone = game.ServerStorage:FindFirstChild("InsertScript"):Clone() local m = Instance.new("ObjectValue",clone) m.Name = "ModelValue" m.Value = script.Parent.Parent.Parent clone.Parent = player.Backpack end end)
Local Script
local model = script:FindFirstChild("ModelValue").Value local player = game.Players.LocalPlayer local character = player.Character wait(5) character:FindFirstChild("Torso").CFrame = model.Part.CFrame script:Destroy()
I tested the script: Works fine. I put two models under the same name with the script in each of them, and the player was teleported to the part I clicked.
A click detector has a parameter. You can access a player though there:
workspace.Part.ClickDetector.MouseClicked:connect(function(player) --See? We can access the player. print(player.Name.." has clicked me!") --We print the player's name. end)
Final script in the part.
script.Parent.ClickDetector.MouseClicked:connect(function(player) if not player.PlayerGui.LocalScript then game:GetService("ServerStorage").LocalScript:clone().Parent = player.PlayerGui end end)
Hope this helps!
It might seem confusing but: Go to the wiki. According to this link http://wiki.roblox.com/index.php?title=MouseClick, under parameter it says that it can find the player who clicked.