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

How to reference a model from a LocalScript?

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.

1 comment

0

could we see the script?

2 answers

✓ Accepted answer

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.

8 comments

0

I like this idea. BUt what if I have two models in the Workspace named the same thing?

0

@ emite1000 , then it won't work.

0

So how should I do it?

0

It will work, actually, since it is an object value instead of a string.

0

Ah okay, I was thinking of a StringValue. I've never worked with Object Values. Thanks for this!

0

No problem.

0

Why are you trying to make the arguments linger? Okay, it's fine now. Stop it.

0

Maybe it would be easier if you shed your hipocrisy and stopped answering with false information :P

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.

2 comments

0

But I need to reference the model they clicked in the LocalScript that I cloned into them. I already did the cloning part.

0

This answer is irrelevant to the question.

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