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

Why doesn't my script detect BoolValue's change?

Asked by 4 years ago

Hello! I have made a Script, which detects if a BoolValue has changed and then checks if it's ticked. Though it does not work, obviously... The BoolValue does get ticked and unticked, but is not detected by this script.

Knife  = script.Parent
KnifeZone = game.ReplicatedStorage.KnifeZone
SwingCheck = Knife.Swinging

if SwingCheck.Value:Changed() then
    print("BoolValue changed!")
    if SwingCheck.Value == true then
        KZclone = KnifeZone:Clone()
        KZclone.Parent = game.Workspace
        KZclone.Position = Knife.Knife.Position
        KZclone.Orientation = Knife.Knife.Orientation
        wait(0.5)
        KZclone:Destroy()
    end
end
0
Extra information. The Knife is in ReplicatedStorage LordTechet 53 — 4y
0
Nevermind! I found what I did wrong and fixed it! Thank you @Feahren and @Elixcore for taking their time to help me! LordTechet 53 — 4y

3 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You’re attempting to reference a method of Value which doesn’t exist. Although there is a method of Instance that does listen for change events, it’s not titled Changed(), but in fact, GetPropertyChangedSignal(). Another similar signal is also the .Changed() event; although applicable in this situation, it only can listen for Values, whereas the separate method can fire for all modified properties—therefore recognized as a stronger practice.

SwingCheck:GetPropertyChangedSignal("Value"):Connect(function()
    print("Value Changed!")
    if SwingCheck then
        --// code Lines 8-13
    end
end)
0
Hmm... LordTechet 53 — 4y
0
I tried my code with that code, still not functioning. The message does not appear LordTechet 53 — 4y
0
Is the program doing what you intended it to do? Ziffixture 6913 — 4y
0
Mhm, the BoolValue gets ticked, but this script doesn't function LordTechet 53 — 4y
0
I assume this is because the Script is attempting to run under ReplicatedStorage? If so, it won’t execute there; it must be relocated to run in a place where it can do so respectively. If it’s a child of a Tool, I suggest migrating said object to the Backpack, and formatting the code into a LocalScript. If not, have the program run as a ServerScript under workspace or ServerScriptService Ziffixture 6913 — 4y
Ad
Log in to vote
0
Answered by
Elixcore 1337 Moderation Voter
4 years ago

Hey, LordTechet. What you are currently doing is trying to use an Event, as a Function.

.Changed cannot be used like you would use something like :Destroy().

It is a special event, also known as RBXScriptSignal.

They function by being combined with 2 different functions, .Changed:Wait(), or Changed:Connect(function).

In this case what you want to do is this:

Change this to an event.

if SwingCheck.Value:Changed() then

Like so:

SwingCheck.Value.Changed:Connect(function()
    --code
end)

Your finished code should look something similar to:

Knife  = script.Parent
KnifeZone = game.ReplicatedStorage.KnifeZone
SwingCheck = Knife.Swinging

SwingCheck.Value.Changed:Connect(function()
    print("BoolValue changed!")
    if SwingCheck.Value == true then
        KZclone = KnifeZone:Clone()
        KZclone.Parent = game.Workspace
        KZclone.Position = Knife.Knife.Position
        KZclone.Orientation = Knife.Knife.Orientation
        wait(0.5)
        KZclone:Destroy()
    end
end)
0
Unfortunately I won't even receive the printed message LordTechet 53 — 4y
0
that means the script is not starting, make sure the script is located somewhere where it would work and also there is no loop before it, or something that will stop it from executing Elixcore 1337 — 4y
0
The script is inside of Knife, which is inside of ReplicatedStorage LordTechet 53 — 4y
0
scripts inside replicatedstorage do not execute, it is just a place for storage, if you want the script to execute you need to bring it out of there. Elixcore 1337 — 4y
0
It gets brought to Workspace by another script LordTechet 53 — 4y
Log in to vote
0
Answered by
Robowon1 323 Moderation Voter
4 years ago

Is the boolvalue being changed by a localscript? If so, the server won't recognize a change in it. To get around this have the local script fire a remoteevent to change the value.

Answer this question