My goal with this script is for it to access a value located in workspace to use in my script.
However for some reason, the script is printing out an error, claiming the value does not exist. Here is my workspace, output, and the script line the error is coming from
The reason I am using :WaitForChild() is because another script makes the object when I call a remote event right before the line of code I show, so it waits for the object to be created in case anything goes wrong in the other script. I tried :FindFirstChild() too in case using :WaitForChild() was the problem but it generated another error (attempt to index nil)
try run the code in series then (one after another) by using remote functions or bindeable events. Also, Maybe it would be better and easier to cache these values in a server-script.
When I do that, the script just tells me the object does not exist in another way but from a line which is later on in the script (error: attempt to perform arithmetic (mul) on number and nil)
That’s because the lines below that are trying to reference the value when you just changed that line to reference an Instance. What you would need to do is remove ‘.value’, create another variable below that defined as:
local petMultiplierValue = petMultiplier.Value
Then you would need to change the references on the lines below to petMultiplierValue instead of petMultiplier. Or alternatively, just add .value at the end of each time you reference its value.
Wow ok this is just not true at all. Find first child is for checking if something exists, the dot operator is for calling objects known to exist, and wait for child is to be used when initially referencing any object (as the script could load before the object does). This person is 100% in the right for using wait for child and it would not change if he used something else (except for if the object did not exist in which case you would get a run time error)
This makes no sense. If the object he is indexing exists, he can just modify its value. There is an issue somewhere in his code meaning that it cannot find the object he is searching for in the reference tree.
The only problem I can think of is perhaps you’re using a server Script in your sword, and the value is being created by a LocalScript, meaning it wouldn’t exist as far as server Scripts are concerned.
I had no idea server scripts cant read objects created by local scripts. I heard somewhere that server scripts cant access folders inside characters so I used a local script and a remote event instead. I still don’t know if it’s true but when I tried it it didn’t work for me so I assumed that.
Yes! LocalScripts cannot make changes on the server. FilteringEnabled was implemented to prevent this practice, as it prevents exploiters from running LocalScripts and pretty much ruining the game for everyone. This is why Remotes are important.
Server scripts are able to see things created by other server scripts. LocalScripts can see anything created by the server or the client it’s running on, but not objects created by LocalScripts on other clients.
In order to do what you’re trying to do, you need to have a server script create and parent the Value object. A LocalScript will then be able to read it. Note that you’ll also need your server script to update the value, as changes made by a LocalScript will not replicate to the server either.
So, for example, in a server script you might have:
local Players = game:GetService("Players")
local function OnPlayerAdded(player)
local multiplier = Instance.new("IntValue")
multiplier.Name = string.format("%sPetMultiplier", player.Name)
multiplier.Value = 1
multiplier.Parent = workspace
end
Players.PlayerAdded:Connect(OnPlayerAdded)
And in a LocalScript:
local Player = game:GetService("Players").LocalPlayer
local Multiplier = workspace:WaitForChild(string.format("%sPetMultiplier", Player.Name))
print(Multiplier.Value)
--> "1"
Thank you. I think I understand what you are trying to say.
I need to go now but I will do that once I am back, I’ll let you know if it works or not when I try it.