Help! Referencing Values Local Script

How do I reference a value inside of a localscript, inside a part from a localscript inside a startergui?
image

image

1 Like

Are those scripts are LocalScripts?

2 Likes

yeah they both are local scripts

1 Like

I’ll rephrase your question:

What you want, is to get the value of value in the second script,
and pass it into the first script
Am I right?

1 Like

Yeah, that is what I am trying to do.

1 Like

The second script:

local thatValue = script.Value -- named it thatValue so it wont be confusing
clicker = script.Parent.ClickDetector -- i assume that the LocalScript with value in it is in the part

function onClicked(mouse)
     clicker.MaxActivationDistance = 0 -- i dont know why are you assinging 2 values at the same time, but i assume thats right
     value.Value = true -- assign thatValue's value to true
     clicker.MaxActivationDistance = 32
end

script.Parent.ClickDetector.MouseClick:Connect(onClicked)

The first script:

wait() -- debounce

local value = -- define the value thats in the LocalScript

if value.Value == true then
     mouse = game.Players.LocalPlayer:GetMouse()
     mouse.Icon = "rbxassetid://46537754"
end

If any error occurs in the output, reply with the screenshot of it

3 Likes

How do you ā€˜define the value that’s in the LocalScript’?

1 Like

Set the path of the value.
In example:

local value = game.Workspace.CursorButton.LocalScript.Value -- assuming that the part(CursorButton) is in the workspace

A question, where does the 1st script belong?

1 Like

Why are you not writing local?

1 Like

The 1st script is belonging in the StarterGui

1 Like

Its not necessary?
I mean, its optional when you’re writing it in the script itself(where no indentation is), and its needed when writing in an if or function space

Thanks!
Full code:

The second script:

local thatValue = game.Players.LocalPlayer.PlayerGui.LocalScript.Value
clicker = script.Parent.ClickDetector -- i assume that the LocalScript with value in it is in the part

function onClicked(mouse)
     clicker.MaxActivationDistance = 0 -- i dont know why are you assinging 2 values at the same time, but i assume thats right
     value.Value = true -- assign thatValue's value to true
     clicker.MaxActivationDistance = 32
end

script.Parent.ClickDetector.MouseClick:Connect(onClicked)

The first script:

wait() -- debounce

local value = script.Value

if value.Value == true then
     mouse = game.Players.LocalPlayer:GetMouse()
     mouse.Icon = "rbxassetid://46537754"
end

Again, if any error occurs in the output, reply with the screenshot of it.

1 Like

There’s no errors, it just doesn’t work. The second script is in Workspace.

Just in workspace or any exact part?

It’s in workspace, CursorButton and it says on line 6 that value is an undefined global variable

Yes, it is an undefined value, since you haven’t called it outside the function.
I’ll rewrite the script, and while I do that, write out the exact name for the part and the script
In example:

-- the part: game:GetService("Workspace").CursorButton (since you've said that the script is in the workspace)
-- the first localscript: game:GetService("Workspace").CursorButton.LocalScript
-- the 2nd localscript: game.Players.LocalPlayer.PlayerGui.LocalScript

The part: first script: game:GetService(ā€œWorkspaceā€).CursorButton
first script: game:GetService(ā€œWorkspaceā€).CursorButton.Localscript
Second Script: game:GetService(ā€œStarterGuiā€).CursorChange

First script:

-- delete everyhting

Second script:

local p = script.Parent.Sound -- assuming that the Sound is in the PlayerGui; if not, set its' path to where it is.
p.Looped = false -- setting it to false because i assume its less than 5 seconds long
local thatScript = game:GetService("Workspace").CursorButton.LocalScript
local value = thatScript.Value
local clicker = script.Value
local mouse = game.Players.LocalPlayer:GetMouse()

thatScript.Parent.ClickDetector.MouseClick:Connect(function()
     if value.Value == true then 
          value.Value = true -- assign thatValue's value to true, since the player clicked on the part
          clicker.MaxActivationDistance = 32
          p:Play()
          mouse.Icon = "rbxassetid://46537754"
     else
          value.Value = false -- assign thatValue's value to false, since the player clicked on the part again
          clicker.MaxActivationDistance = 32
          -- not calling the sound because the value is false
     end
end)

You would have to create a value inside the second script(dont name it, keep its’ name Value), and delete the first script since the function is inside of the second script.
Reply if nothing happens, or an error occurs.
If you want exactly 2 scripts to be made, reply with saying so.

update: changed the script since i used a different variable