Why can't it add +150 value to the Cash numbervalue?

I am trying to make it where every time inround == false.

		local function loadPlayerData(player)
			local key = "Player_" .. player.UserId

			local success, data = pcall(function()
				return dataStore:GetAsync(key)
			end)

			if success and data ~= nil then
				player.leaderstats.Cash.Value = data
			end
		end

		-- Function to initialize leaderstats for a player
		local function initializeLeaderstats(player)
			-- Create leaderstats folder
			local leaderstats = Instance.new("Folder")
			leaderstats.Name = "leaderstats"
			leaderstats.Parent = player

			-- Create Cash value
			local cash = Instance.new("NumberValue")
			cash.Name = "Cash"
			cash.Value = 500
			cash.Parent = leaderstats
		end

		-- Connect the functions to player events
		game.Players.PlayerAdded:Connect(function(player)
			initializeLeaderstats(player)
			loadPlayerData(player)
		end)

This is what makes the cash.

I made an inround value to check if the inround is false.

And if it is this script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local InRound = ReplicatedStorage:WaitForChild("InRound")
local player = game.Players.LocalPlayer

if InRound.Value == false then
	local msg = Instance.new("Message", workspace)
	msg.Text = "Rewarded 150 Cash!"

	local leaderstats = player:WaitForChild("leaderstats")
	local cash = leaderstats:WaitForChild("Cash")

	cash.Value = cash.Value + 150
else
	error("Can't find the player cash!")
end

Is meant to add +150 cash.

It won’t do so?

Heres a video too

Can anyone help?

1 Like

I see two main issues.

  1. Your RewardHandler script is a local script inside ServerScriptService, so that will never run.

  2. Scripts only run once when the game first loads or when they are first required. That if statement will only be checked once and never run again. You need to put it in a function that can be called or connected to an event so that it can check when the round starts and add the cash each time.

I added the local script in the StarterPlayerScripts.

image

Second I updated the code

local function checkRoundStatus()
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local InRound = ReplicatedStorage:WaitForChild("InRound")
	local player = game.Players.LocalPlayer

	if InRound.Value == false then
		local msg = Instance.new("Message", workspace)
		msg.Text = "Rewarded 150 Cash!"

		local leaderstats = player:WaitForChild("leaderstats")
		local cash = leaderstats:WaitForChild("Cash")

		cash.Value = cash.Value + 150
	else
		error("Can't find the player cash!")
	end
end

game.Players.PlayerAdded:Connect(function(player)
	checkRoundStatus()
end)

Is this good enough?

Not quite.

Firstly it doesn’t make sense to use PlayerAdded for this purpose in a local script because you already have the local player.

Secondly you want to try add cash each time a round starts, this will only add cash when the player first joins.

You need to call this function for every player each time the round starts.

Another issue is that you can’t update the cash value on the client, as the changes won’t occur on the server.

Should I just use a remote event then?

I don’t see why one is needed.

The server decides when a round starts, so you can just have the server add cash to every player when it does.

My Roblox Studio is too laggy, might take time to make changes if you do suggest some.

Hey, I was able to make it work now!
Thanks for your time.

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