Attempt to index nil with "Value" but object is not nil

I’m trying to make it so it takes away money when buying an upgrade

Local script

local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Up:Connect(function()
	local up = script.Parent.Parent.Upgrade
	local cost = script.Parent.Parent.Cost
	if plr.leaderstats.Money.Value >= cost.Value then
		game.ReplicatedStorage.Upgrade:FireServer(plr, up, cost)
	end
end)

Server script

game.ReplicatedStorage.Upgrade.OnServerEvent:Connect(function(plr, up, cost)
	if plr.leaderstats.Money then --if statement shows it's there
		plr.leaderstats.Money.Value = plr.leaderstats.Money.Value - cost.Value --error
		up.Value.Value = up.Value.Value + 1
	end
end)

The if statement clearly shows that it’s there, but the next line shows that it’s nil

Tell me if there is anything I’am doing wrong.

Value is probably not a property of money, so it’s probably not an int/number value.

try this maybe gonna work, -= should work :smile:

Edit: dont work i tried ;(

It’s because you’re sending the player as an argument on the client’s side, which is unnecessary since the player is a built-in argument on the server’s side. The server is registering this:

Upgrade.OnServerEvent:Connect(function(playerWhoFired, plr, up, cost)
    -- Blah, blah.
end)

But you’re only picking up the first two values sent from the client, because the first argument on the server is already occupied – the first argument is the player who fired the server, an automatic argument.

So, your server script looks like this:

Upgrade.OnServerEvent:Connect(function(playerWhoFired, plr, up) end)

To fix this, just remove the plr argument the client is sending.

Also, if the ValueObject was created by the client, it is not replicated to the server. So, the server does not know what that ValueObject, referred to as cost, is.
The costs should be stored on the server, and the client should only fire to the server what the name of the upgrade is. Then, from the server, you can validate the name and then subtract the cost of the upgrade with the given name from the client’s money. This will prevent exploiting.

the error is there you cant pick the value of money i think

1 Like

soo you need change

	plr.leaderstats.Money.Value = plr.leaderstats.Money.Value - cost.Value --error

to

plr.leaderstats.Money = plr.leaderstats.Money - cost.Value

Thanks for pointing that out, but I tried it and it didn’t work.

Sorry guys, but it was the cost, not the money that was nil.

1 Like

so it dont work?
what you mean

1 Like

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