How to update a value in a script

I have a system of luck boost and inventory and if the player has more than the player can store in his inventory that do nothing, but idk how can I update the value

The value read in the script is the value when I loggin into the game and not the value when Im playing and when it is beeing changed

task.wait(4)

local LPlayer = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local CollF = workspace.Collectibles
local BoostCount = LPlayer.BoostCount.Value

local PopUpFrame = LPlayer.PlayerGui:WaitForChild("PopUpGUI"):WaitForChild("MainFrame")
local PopUpText = PopUpFrame:WaitForChild("MainLabel")

local function trigger(instance, spawnLocation)
	local main = instance.Parent
	instance.Triggered:Connect(function(plr)
		local BoostF = plr.Boosts
		if BoostF then
			local potion = BoostF:FindFirstChild(main.Name)
			if potion then
				local luckboost = plr.LuckBoostTaken:FindFirstChild(spawnLocation)
				if luckboost then	
					if BoostCount <= 0 then
						RS.Remotes.BoostNotTaken:FireServer(potion, luckboost)
						RS.Remotes.StoreBoost:FireServer(potion.Name)
						main:Destroy()
						print(BoostCount)
					elseif BoostCount >= plr.MaxBoost.Value then
						PopUpText.Text = "You've reached the max amount of boost !"
						PopUpText:TweenPosition(UDim2.new(0,0,0,0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.35, true)
						wait(1.55)
						PopUpText:TweenPosition(UDim2.new(0,0,1,0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.35, true)
						return
					end
				end
			end
		end
	end)
end

for _, part in pairs(CollF:GetChildren()) do
	local prompt = part:FindFirstChild("ProximityPrompt")
	if prompt then
		trigger(prompt, part.Spawnpoint.Value)
	end
end

if you guys can help me I’ll really appreciate it :slight_smile:

1 Like

There’s easy way of fixing this. You have to not addres the value at beggining

local BoostCount = LPlayer.BoostCount.Value

this makes it So BoostCount is only number, like 0 when player joins, it’s not listening to changes. You just have to make it so

local BoostCount = LPlayer.BoostCount -- So it is looking at the variable and then check what's the value when needed
BoostCount.Value -- like this

What’s with the wait? You don’t need this.

I think he’s making sure BoostCount has been added to player’s folder.

Why not just use :WaitForChild()?

idk, but you’re right WaitForChild() is probably more efficient

I need it because the map is spawning like 3 second after and the script is working with the map

task.wait(4)

local LPlayer = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local CollF = workspace.Collectibles
local BoostCount = LPlayer.BoostCount

local PopUpFrame = LPlayer.PlayerGui:WaitForChild("PopUpGUI"):WaitForChild("MainFrame")
local PopUpText = PopUpFrame:WaitForChild("MainLabel")

local function trigger(instance, spawnLocation)
	local main = instance.Parent
	instance.Triggered:Connect(function(plr)
		local BoostF = plr.Boosts
		if BoostF then
			local potion = BoostF:FindFirstChild(main.Name)
			if potion then
				local luckboost = plr.LuckBoostTaken:FindFirstChild(spawnLocation)
				if luckboost then	
					if BoostCount.Value <= 0 then
						RS.Remotes.BoostNotTaken:FireServer(potion, luckboost)
						RS.Remotes.StoreBoost:FireServer(potion.Name)
						main:Destroy()
						print(BoostCount)
					elseif BoostCount.Value >= plr.MaxBoost.Value then
						PopUpText.Text = "You've reached the max amount of boost !"
						PopUpText:TweenPosition(UDim2.new(0,0,0,0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.35, true)
						wait(1.55)
						PopUpText:TweenPosition(UDim2.new(0,0,1,0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.35, true)
						return
					end
				end
			end
		end
	end)
end

for _, part in pairs(CollF:GetChildren()) do
	local prompt = part:FindFirstChild("ProximityPrompt")
	if prompt then
		trigger(prompt, part.Spawnpoint.Value)
	end
end

I wrote this now but thats still not working idk

actually it is working btw sorry and ty

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