Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to reference a model from a LocalScript?

Asked by
emite1000 335 Moderation Voter
9 years ago

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.

0
could we see the script? woodengop 1134 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

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.

0
I like this idea. BUt what if I have two models in the Workspace named the same thing? emite1000 335 — 9y
0
@ emite1000 , then it won't work. EzraNehemiah_TF2 3552 — 9y
0
So how should I do it? emite1000 335 — 9y
0
It will work, actually, since it is an object value instead of a string. aquathorn321 858 — 9y
View all comments (4 more)
0
Ah okay, I was thinking of a StringValue. I've never worked with Object Values. Thanks for this! emite1000 335 — 9y
0
No problem. aquathorn321 858 — 9y
0
Why are you trying to make the arguments linger? Okay, it's fine now. Stop it. EzraNehemiah_TF2 3552 — 9y
0
Maybe it would be easier if you shed your hipocrisy and stopped answering with false information :P aquathorn321 858 — 9y
Ad
Log in to vote
-1
Answered by 9 years ago

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.

0
But I need to reference the model they clicked in the LocalScript that I cloned into them. I already did the cloning part. emite1000 335 — 9y
0
This answer is irrelevant to the question. aquathorn321 858 — 9y

Answer this question