Script will not compare intvalues

I have this script with the goal being that once you hit “20 strength” it gives you a six pack, but the script simply wont work, no errors or anything.

local StarterPlayer = game:GetService("StarterPlayer")
local SixPack = StarterPlayer.StarterCharacter.SixPack
local Player = game.Players



Player.PlayerAdded:Connect(function(player)
	SixPack.Transparency = 1
	
	local Strength = player:WaitForChild("Strength")
	local Int = Strength.StrengthStats
	
	while true do
		wait(0.001)
		if Int.Value == 20 then
			SixPack.Transparency = 0
		end
	end
end)

Use Int.Changed. Spamming a loop only slows down game performance.


Int.Changed:Connect(function(Val)
   if Val == 20 then
       — run code
   end
end)

Repeatedly checking that value for every player is a bad idea. Instead, just check it when the value changes, which is more efficient:

local function intChanged(newIntValue)
	if newIntValue.Value == 20 then
		SixPack.Transparency = 0
	end
end

Player.PlayerAdded:Connect(function(player)
	SixPack.Transparency = 1
	local Strength = player:WaitForChild("Strength")
	local Int = Strength.StrengthStats

	Int.Changed:Connect(intChanged)	
end)

It is giving me an error,

ServerScriptService.BodyLooks:8: attempt to index number with ‘Value’ - Server - BodyLooks:8
20:53:04.659 Stack Begin - Studio
20:53:04.659 Script ‘ServerScriptService.BodyLooks’, Line 8 - function intChanged - Studio - BodyLooks:8
20:53:04.659 Stack End - Studio

Basically saying that there is something wrong with line 8. Something to do with the .Value

newIntValue is a string, not a intValue.
Use :GetPropertyChangedSignal, it’s much better in this case.

Player.PlayerAdded:Connect(function(player)
	SixPack.Transparency = 1
	local Strength = player:WaitForChild("Strength")
	local Int = Strength.StrengthStats

	Int:GetPropertyChangedSignal("Value"):Connect(function()
       if Int.Value == 20 then
          SixPack.Transparency = 1
       end
    end
end)

Also, where is the SixPack variable?

Remove the .Value from newIntValue

local function intChanged(newIntValue)
	if newIntValue == 20 then
		SixPack.Transparency = 0
	end
end

Player.PlayerAdded:Connect(function(player)
	SixPack.Transparency = 1
	local Strength = player:WaitForChild("Strength")
	local Int = Strength.StrengthStats

	Int.Changed:Connect(intChanged)	
end)

This does not work. Does the Int.Changed only happen once, or can it be activated at anytime?

The original script that BadDad2004 posted only makes Int.Changed run once, as it only activates once the player has joined.

I’m pretty sure the reason as of why this doesn’t work is because you are trying to change the transparency of the SixPack from StarterCharacter (that is in StarterPlayer) instead of doing it to the player.


local function intChanged(CharSixPack, newIntValue)
	if newIntValue == 20 then
		CharSixPack.Transparency = 0
	end
end

Player.PlayerAdded:Connect(function(player)
    local SixPack = player.CharacterAdded:Wait().SixPack

	SixPack.Transparency = 1
	local Strength = player:WaitForChild("Strength")
	local Int = Strength.StrengthStats

	Int.Changed:Connect(intChanged(SixPack, Int.Value))	
end)

The problem is your setting the StarterCharacter’s Sixpack which won’t change the actual player’s and instead just whoever spawns in next. Try this code and try to understand it and how it wokrs.

local StarterPlayer = game:GetService("StarterPlayer")
local Player = game.Players

local function intChanged(newIntValue, CharSixPack)
	if newIntValue == 20 then
		CharSixPack.Transparency = 0
	end
end

Player.PlayerAdded:Connect(function(player)
	local Character = player.Character or player.CharacterAdded:Wait()
	local SixPack = Character:WaitForChild("SixPack")
	local Strength = player:WaitForChild("Strength")
	local Int = Strength:WaitForChild("StrengthStats")

	SixPack.Transparency = 1

	Int:GetPropertyChangedSignal("Value"):Connect(function()
		intChanged(Int.Value, SixPack)
	end)
end)

Dont worry, I shall learns the ways of this script. (It works!)

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