RemoteEvent Not Finding My PlayerValues

The way the script should be functioning is once a player clicks the button it should basically add the item they are buying and subtract money.

For some reason I am getting an error.

Script 1 part of The Button that fires the Event

local btn = script.Parent
local RemoteEvent = game.ReplicatedStorage.ItemBought
local PaperClipValue = script.Parent.Parent.Parent.BuyFrame.Buy.IPPPaperClipalue
local Cash = game.Players.LocalPlayer.Values.Cash
local Item = game.Players.LocalPlayer.Values.PaperClips

btn.MouseButton1Down:Connect(function()
	if PaperClipValue.Value*5 > Cash.Value then 
		print("No sir you need more cash")
	elseif PaperClipValue.Value*5 <= Cash.Value then
		RemoteEvent:FireServer(PaperClipValue.Value,5,Item)
	end
end)

Script 2 (One with Error) Handles the Adding and Subtracting of Values

local RemoteEvent = game.ReplicatedStorage.ItemBought

RemoteEvent.OnServerEvent:Connect(function(plr,AmountOfItemPurchased,ItemPrice,Item)
	plr.Values.Cash.Value -= AmountOfItemPurchased*ItemPrice
	plr.Values.Item.Value += AmountOfItemPurchased
end)

P.S - I printed ā€˜Item’ before it does Print the Value that I want being bought

Values Script If needed

game.Players.PlayerAdded:Connect(function(player)
	
	local Values = Instance.new("Folder")
	Values.Name = "Values"
	Values.Parent = player
	
	--- Leaderstats Below
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = Values
	
	local Paper = Instance.new("IntValue")
	Paper.Name = "Paper"
	Paper.Parent = Values
	
	local PaperClips = Instance.new("IntValue")
	PaperClips.Name = "PaperClips"
	PaperClips.Parent = Values
	
	local Pens = Instance.new("IntValue")
	Pens.Name = "Pens"
	Pens.Parent = Values

	local Markers = Instance.new("IntValue")
	Markers.Name = "Markers"
	Markers.Parent = Values
	
end)
1 Like

Please include what error you are receiving. It’s hard to help you without knowing what the problem is.

Could it be that plr.Values.Itembeingpurchased is nil? I don’t see that Instance listed in your values/leaderstats script.

If you mean to specifically grab the value depending on what item was purchased, you need to include that in your event parameters.

  • Send the item name with the event fire.

    RemoteEvent:FireServer(PaperClipValue.Value,5,Item,Item.Name)

  • Receive the item name as ItemBeingPurchased variable

    RemoteEvent.OnServerEvent:Connect(function(plr,AmountOfItemPurchased,ItemPrice,Item,ItemBeingPurchased)

  • Wait until the Values folder contains the specified IntValue, then applies the new amount.

    plr.Values:WaitForChild( tostring(ItemBeingPurchased) ).Value += AmountOfItemPurchased

3 Likes

I have it you read the script included the value.

So I don’t think that’s the problem.

I have a screenshot on my computer that I meant to put but I guess forgot it’s currently out of battery but I will post that in the morning.

It’s something about that line 6 value is coming as nil or something like that.

In the 2nd script

1 Like

say the error then my buddy

1 Like

@no_tsi
I’m sorry as stated above if it helps.

@HeyWhatsHisFace
@no_tsi

Here is the error.

You’re trying to call plr.Values.Itembeingpurchased and I don’t see anywhere that it was created in your scripts

2 Likes

Item was called in the first script I changed the name because I thought I would get a different result.

It is stupid but Item was called here.

Could the problem possible be that you can’t do that with a remote event like is it even possible?

My initial response addressed this error and how to fix it. The error is that ā€œItembeingpurchasedā€ is nil.


To clarify:
You can’t index objects with other objects.

I see you defined an Itembeingpurchased variable but that is a reference to the item object. You can’t index objects with variables or other objects. You have to specify its exact name or search with strings using :FindFirstChild() / :WaitForChild(). ā€œItembeingpurchasedā€ is what is being searched for in the plr.Values folder since that is the exact name you provided, and is returning nil since it can’t find an item called ā€œItembeingpurchasedā€.


You could change the line to:

plr.Values[Itembeingpurchased.Name].Value += AmountOfItemPurchased

but I do not recommend that because theres a chance your Itembeingpurchased variable could go nil at any time, breaking the script. Or it could possibly not locate an item with Itembeingpurchased’s Name, then it would cause a similar error trying to index nil with ā€œValueā€.

I recommend following my initial reply:

	local btn = script.Parent
	local RemoteEvent = game.ReplicatedStorage.ItemBought
	local PaperClipValue = script.Parent.Parent.Parent.BuyFrame.Buy.IPPPaperClipalue
	local Cash = game.Players.LocalPlayer.Values.Cash
	local Item = game.Players.LocalPlayer.Values.PaperClips
+	local ItemName = Item.Name

	btn.MouseButton1Down:Connect(function()
		if PaperClipValue.Value*5 > Cash.Value then 
			print("No sir you need more cash")
		elseif PaperClipValue.Value*5 <= Cash.Value then
+			RemoteEvent:FireServer(PaperClipValue.Value,5,Item,ItemName)
		end
	end)
	local RemoteEvent = game.ReplicatedStorage.ItemBought

+	RemoteEvent.OnServerEvent:Connect(function(plr,AmountOfItemPurchased,ItemPrice,Item,ItemBeingPurchased)
		plr.Values.Cash.Value -= AmountOfItemPurchased*ItemPrice
+		plr.Values:WaitForChild( tostring(ItemBeingPurchased) ).Value += AmountOfItemPurchased
	end)
1 Like

So with this as well I could drop the Item in this code line here.

Since its not used here

this code doesnt even make any sense

why are you creating a variable for item when it already exists (and not even using it)
that is extra processing power for absolutely zero reason

and the error is probably explaining itself very well, there is no value called itembeingpurchased and i dont see you creating it anywhere in your scripts

This is because it wasn’t working with Item so I decided to try another stupid way that didn’t work so I came to deform and forgot to change it. Sorry Im dumb.

It is you just need to fully read the scripts.

Thank you so much for your help I’m so sorry that I suck at scripting one day I hope to get better thanks for helping me understand more even though at times it might have been difficult for you to understand me.

I edited the script a little but for the most part you guided me well.

Thank you!

1 Like