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

How would I format the end of a function like this?

Asked by 9 years ago

What I want to do is make a Changed function. The function will fire when a value in a player's inventory is changed, but the problem is that the value doesn't constantly exist in the player's inventory. So putting something like this inside a localscript:

function change()
--code here
end

game.Players.LocalPlayer.Value.Changed:connect(change)

will break since sometimes the value is nil. And when I put something like this:

function change()
--code here
end

if game.Players.LocalPlayer.Value then
    game.Players.LocalPlayer.Value.Changed:connect(change)
end

the function just doesn't work. So, how would I format this?

0
Just so you know using Value as a value name will only serve to confuse you Mystdar 352 — 9y

3 answers

Log in to vote
1
Answered by 9 years ago

Your problem may be that Value may not be existant at the time of the code executing, consider using the WaitForChild method, for the WaitForChild method will repeat waiting until the Child is existant, and will return it when so, also, let's add an Identifier to your code so that you don't have to keep using game.Players.LocalPlayer.Value over and over again, to make it a bit more efficient, now, let's fix up your code;

repeat wait() until game.Players.LocalPlayer --This will repeat waiting until 'LocalPlayer' is existant, LocalPlayer is the Player the LocalScript is running for, as said on the WIKI
local Value = game.Players.LocalPlayer:WaitForChild("Value") --This will repeat waiting until 'Value' is existant within the Player, and will return it, once it exists, identifier 'Value' will, well, identify it

function change() --This is your original function
if Value and Value:IsA("StringValue") then --I am assuming this is a 'StringValue' type instance; The 'if then end' statement will check the conditions for a block of code; This will check to see if 'Value' is existant, and 'Value' is a 'StringValue' type instance
print("Function 'change' fired! :D") --This will print if the conditions are/were met; This will print the string 'Function 'change' fired! :D' into the Output
end --This ends the code block for the 'if' statement
end --This ends the code block for the function

game.Players.LocalPlayer.Value.Changed:connect(change) --Now, whenever something has changed within 'Value' [Name, Parent, Value, ect.], the event '.Changed' will fire the function 'change'

Hope this helped, and Happy New Years!

0
This works, thanks man! whyOmustOitObeOme 7 — 9y
Ad
Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Yes, your script would error due to the value being nil at some moments. But to get around this, we can make a sort of custom changed function.

We can do this by setting an 'oldVal' variable. Then having a While loop, followed by the checking of IF the value is nil. If it isn't nil then we continue with the script to compare the 'oldVal' with the currentVal. Then if there's a change we can put some code in(:

Somthing like this:

local val = game.Players.LocalPlayer:FindFirstChild("ValueName")
local oldVal = 0

while wait(1) do --to reduce lag, have a 1 second interval.
    if val then
        if oldVal ~= val.Value then
            oldVal = val.Value
            --Your Code--
        end
    end
end
Log in to vote
-2
Answered by 9 years ago

This may work im not sure

function change()
--code here
end
while true do
wait(0.1)-- Stops script from crashing
if game.Players.LocalPlayer.Value.Changed then--when localplayer value changes then it will activate the function change
change()--change bieng activated
    end
end

may not need extra end

Answer this question