! | problem with intvalue if statements

Goal:

  • The script should check if a player’s IntValue (called Insanity) is
  • if it is, It will check if it is nighttime [working]
  • if that is true, the script will give the player the knife [working but when i add the intvalue checker it does not]

My script:

local players = game:GetService("Players")
local InsanityValue = players:WaitForChild("Insanity")

InsanityValue.Changed:Connect(function(val)

	if InsanityValue == 4 then

		lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
			local clockTime = lighting.ClockTime

			if clockTime <= 5 or clockTime >= 20 then
				giveKnives()
			elseif clockTime >= 5 or clockTime <= 20 then
				removeKnives()

			end
		end)
	end
end)

Notes:

  • The giveknives() and removeknives() function is already made
  • The script works without the insanityvalue checker
  • The IntValue works correctly

Locations:

  • The script above:
    image

  • The Insanity intValue:
    image

Does anyone have any ideas, please? c:

1 Like

I’m thinking, I will edit this post when I found out the problem.

Is the insanity value being set to four? Try

InsanityValue += 4

Make sure to put it outside of the value changed function.

@triisflame

1 Like

An instance cannot be “4”. If InsanityValue is the said IntValue, you need to add .Value.

local players = game:GetService("Players")
local InsanityValue = players:WaitForChild("Insanity")

InsanityValue.Changed:Connect(function(val)

	if InsanityValue.Value == 4 then

		lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
			local clockTime = lighting.ClockTime

			if clockTime <= 5 or clockTime >= 20 then
				giveKnives()
			elseif clockTime >= 5 or clockTime <= 20 then
				removeKnives()

			end
		end)
	end
end)
3 Likes

That’s what I thought at first but I remembered a leaderstats script I made without the value and-

Oh yeahhhhh

You did not specify the player in the script. Because of that, the game looks for an instance called “InsanityValue” inside Players, not a specific player.

if InsanityValue.Value == 4 then

May as well use the ‘val’ parameter.

if val == 4 then
2 Likes
  1. Create a RemoteEvent in ReplicatedStorage called “InsanityCheckEvent”

  2. LocalScript in PlayerScripts:

local plr = game:GetService("Players").LocalPlayer
local InsanityValue = plr.InsanityValue
local event = game:GetService("ReplicatedStorage"):FindFirstChild("InsanityCheckEvent")

InsanityValue:GetPropertyChangedSignal("Value"):Connect(function()
	if InsanityValue.Value >= 4 then
		local val = InsanityValue.Value
		event:FireServer(plr, val)
	end
end)
  1. ServerScript in ServerScriptService:
local event = game:GetService("ReplicatedStorage"):FindFirstChild("InsanityCheckEvent")

event.OnServerEvent(plr,val):Connect(function()
	if val == 4 then
		lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
		local clockTime = lighting.ClockTime

		if clockTime <= 5 or clockTime >= 20 then
			giveKnives()
		elseif clockTime >= 5 or clockTime <= 20 then
			removeKnives()

		end
	end)
	end
end)
1 Like

Thank you so much for the instructions, I really appreciate it!

After following your steps, there was an error in the ServerScript around the plr, val parameter. Do you have any idea on how I can fix it? (I have the event:FireServer(plr, val) in the player script so I’m not too sure as to why)

Here is the error it shows in my output:

local event = game:GetService("ReplicatedStorage"):FindFirstChild("InsanityCheckEvent")

event.OnServerEvent():Connect(function()
		lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
		local clockTime = lighting.ClockTime

		if clockTime <= 5 or clockTime >= 20 then
			giveKnives()
		elseif clockTime >= 5 or clockTime <= 20 then
			removeKnives()

		end
	end)
end)

We don’t actually need the plr, val because we checked it in the local script. Of course you need to edit the script a bit so it fits to what you’re trying to do.

Remove the plr, val from the .FireServer too.

the OnServerEvent Event doesn’t work like that.

event.OnServerEvent:Connect(function(plr,val)

Looks like you’re comparing the object to a number. You need to get the number of the value. Other than that, there’s some other things you may want to consider for optimizing the code.

Assuming you want the knife giving & removing to only work between specific times, you should bind it to a connection and disconnect it when it is not needed.

local players = game:GetService("Players")
local lighting = game:GetService("Lighting")
local insanity = players:WaitForChild("Insanity")
local connection: RBXScriptConnection

insanity:GetPropertyChangedSignal("Value"):Connect(function()
	local value = insanity.Value
	if value == 4 then
		connection = lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
			local ct = lighting.ClockTime
			if ct <= 5 or ct >= 20 then
				giveKnifes()
			else
				removeKnifes()
			end
		end)
	elseif typeof(connection) == 'RBXScriptConnection' then
		return connection:Disconnect()
	end
end)

my bad sorry, i was writing this on my phone so i didnt get any auto formatting

chrlmt