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

Why does code placement matter so much with variables?

Asked by 2 years ago

I was making a weapon and one of the first things I needed to get was the mouse pos. not very hard to do, it should've taken me 1 min but instead took me 3 hours. This was the original code I was working with that didn't work - the only difference is that the 1st EX used 2 vars to get the mouse P and the 2nd EX used 1 var to get the mouse P

Local Script OG code This code would only print out a single position value even when the mouse was obviously not near that position

local RE = script.Parent.Parent:WaitForChild("mouse")
local tool = script.Parent.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local hit = mouse.Hit     *the difference*

tool.Activated:Connect(function(plr)
    local position = hit.Position    *the difference*
    RE:FireServer(position)
end)

Local script This code actually printed the correct position of the mouse no matter where it went

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local RE = script.Parent.Parent:WaitForChild("mouse")
local tool = script.Parent.Parent

tool.Activated:Connect(function(plr)
    local position = mouse.Hit.Position    *the difference*
    RE:FireServer(position)
    wait()
end)

My question is that why does code placement matter so much if, in my example "the difference" in the code really wasn't different, both sections of code should've executed the same results. if I'm wrong, why does the placement matter so much????

2 answers

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

Here is a small concept most of the scripters might confuse about:

local a = 2
local b = a

a = 5
print(b)

If you expected the output to be 5, that's incorrect. Variables are not shortcuts, so if you do local b = a, it does not mean the b in your script is going to be the same value as a.


But why?

Let's use a similar concept, and I'm going to split the task one by one so you can keep up with what I'm trying to say.

Imagine variables are like storage:

  • You have two boxes, one is A and one is B.

  • You put two apples inside box A, and you put the same amount of apples inside box B.

  • Now you change the number of apples in box A to 5,

  • Box B still stays the same amount of apples, which is 2.

Why is box B not 5? Because you didn't update the number of apples in box B. Let's get back to the code:

local a = 2
local b = a

a = 5
b = a -- updates the b variable
print(b)

That's how you have the b variable as 5! If this sounds really confusing, consider bookmarking this answer and you can come back once you are a little more experienced.


Looking back at your code, the mistake you did was not updating the hit variable, here will be what you should've done if you use my logic:

local RE = script.Parent.Parent:WaitForChild("mouse")
local tool = script.Parent.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local hit = mouse.Hit

tool.Activated:Connect(function(plr)
    hit = mouse.Hit -- updates the hit variable
    local position = hit.Position
    RE:FireServer(position)
end)

Ad
Log in to vote
1
Answered by
Puppynniko 1059 Moderation Voter
2 years ago
Edited 2 years ago

im not really good at explaining it but if the position is outside of the function when the script first ran it would take the current position and place it in the variable(will never change in your script) but if its inside the variable gets updated with the new Position Edit: I forgor to click the answer button

Answer this question