LocalScript not detecting value in an if statement

Just as the title says. I have no idea why this happens since this is the first time that this has ever happened to me. Value is added through remote events so datasave can save the data.

LocalScript inside replicatedfirst (musicon value is on 1 but it still plays the sound):

local player = game.Players.LocalPlayer
local MusicNumber = player:WaitForChild("Stats"):WaitForChild("MusicNumber")
local MusicON = player.Stats:WaitForChild("MusicON")
local Waiter = player:WaitForChild("Stats").Waiter
local RS = game:GetService("ReplicatedStorage")
local WaiterAdder = RS.Settings:WaitForChild("WaiterAdder")
local MusicNumberAdder = RS.Settings:WaitForChild("MusicNumberAdder")
local SoundService = game:GetService("SoundService")
local Adventure1 = SoundService.Adventure1
local Buddy2 = SoundService.Buddy2
local Poolside3 = SoundService.Poolside3
local Skyline4 = SoundService.Skyline4
local Journey5 = SoundService.Journey5
local ONButton = player.PlayerGui.SystemGUI2.MainFrame.Menu.SettingsFrame.Music.ON
local OFFButton = player.PlayerGui.SystemGUI2.MainFrame.Menu.SettingsFrame.Music.OFF

if MusicON.Value == 20 then
	Adventure1:Play()
	ONButton.BackgroundColor3 = Color3.fromRGB(63, 255, 33)
	OFFButton.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
	Adventure1.Ended:Wait()
	WaiterAdder:FireServer()
	wait(0.5)
end
4 Likes

Check if the MusicON value is being updated correctly through the remote event, make sure the variable is of the correct type, verify the path to the variable, add debug statements, and consider using the ValueChanged event for accurate response to updates.

1 Like

The check is probably happening before the value is changed. That’s my guess. I don’t know how you would fix it because I don’t know what your code on the server looks like. Posting that would be very helpful :+1:

2 Likes

Regarding your first bit I just put the number 20 there as a test to see if the script would still run. The value never hits 20 but the script detects so.

This is where i add the value:

local RS = game:GetService("ReplicatedStorage")
local MusicONAdder = RS.Settings:WaitForChild("MusicONAdder")

MusicONAdder.OnServerEvent:Connect(function(player)
	local MusicNumber = player.Stats:WaitForChild("MusicNumber")
	local MusicON = player.Stats:WaitForChild("MusicON")
	if MusicNumber.Value == 0 then
		MusicON.Value = 1
	elseif MusicNumber.Value == 1 then
		MusicON.Value = 2
	elseif MusicNumber.Value == 2 then
		MusicON.Value = 3
	elseif MusicNumber.Value == 3 then
		MusicON.Value = 4
	elseif MusicNumber.Value == 4 then
		MusicON.Value = 5
	end
end)
1 Like

Pretty sure everything is correct, and the ValueChanged event wouldn’t work in this case because the script only runs once whenever the player joins before any value is set.

1 Like

I’m a bit confused but I think what you have to do is use the :GetPropertyChangedSignal() connection. Something like:

MusicOn:GetPropertyChangedSignal("Value"):Connect(function()
--insert if statement stuff etc
end)

Correct me if I’m not understanding something…

1 Like

Can you explain what is going wrong? I’m super confused lolll

1 Like

does changing the MusicON value allow you to play a different soundtrack or something?

1 Like

you can use the ValueChanged event to detect changes to the MusicON value. by listening to this event, the LocalScript will be able to react and update the sound playing based on the updated value, resolving any timing mismatch between the value update and the script execution.

1 Like

Yeah sorry if I wasn’t understandable. So all in all this script is a rejoin checker to see if the player put the music to on or off in the settings. This is the ON part of it. The problem is I can put anything I want in the if statement like player.Character.Humanoid.MaxHealth == 1 the script would STILL run. This has nothing to do with the MusicON value. Something else is the case.

1 Like

can you show the heirachy in the explorer?

1 Like

Yes.
This is the 1st script where the problem is:
yoa

MusicON is checking whether the music is on or off right? if so why did you check if MusicON is 20?

1 Like

Like I said, I just put the number 20 there as a test to see if the script would still run. The problem is I can put anything I want in the if statement like player.Character.Humanoid.MaxHealth == 1 the script would STILL run. This has nothing to do with the MusicON value. Something else is the case.

2 Likes

Can you put a print(“Testing”) before the if statement and a print(“Running”) inside the if statement?

2 Likes

yeah i was just clarifying

you also added this as a demonstration right?

1 Like

Yeah ty for that. Because of that I realised what is the problem. When I duplicated the script from startergui to replicatedfirst to test if that would work I didn’t disable the script in the startergui. So both of them ran. The script in the replicatedfirst now works. Well sorry for taking your time with my silly little mistake. <3

2 Likes

It’s alright. If you have any other questions feel free to message me.

1 Like

Just a note here instead of having a ton of conditions, you can also just do MusicON.Value = math.clamp(MusicNumber.Value + 1, 0, 5)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.