Having trouble calling a numbervalue inside of an object from a server script

Basically I’m trying to create an eat/drink remote which for the most part worked until I added in an “amount” that limits the amount of food you can get from one food object. The idea was that if the Amount was less than or equal to 0 than the food object would be destroyed from the workspace. The only problem im having is that im not able to call the Amount Value which is inside of the food object, from the serverscript. I’m getting this error: attempt to index nil with ‘Value’.

There is a folder inside of the player named “Stats” which contain the NumberValues “Hunger” and “Thirst”

Does anyone have a better solution I can use to call a numbervalue inside of an object from a serverscript? It would be much appreciated.

Whenever the player hovers over an object named “Leaf” and pressed the Key “F” the Eat remote will be fired.

Heres the localscript code:

local UserInputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Events = game.ReplicatedStorage:WaitForChild("Events")


local Limit = 15


local EatKey = Enum.KeyCode.F


local function IsEatKeyDown()
	return UserInputService:IsKeyDown(EatKey) 
end

local function Input(input, gameProcessedEvent)
	if not IsEatKeyDown() then
		return
	else
		
		if Mouse.Target.Name == "Leaf" then
			local distance = (Mouse.Hit.p - Player.Character.HumanoidRootPart.Position).Magnitude
			if distance < Limit then
				if Mouse.Target.Amount.Value <= 100 then
					if Mouse.Target.Amount.Value <=   0 then
						Events.RemoteEvent:FireServer("Eat", Mouse.Target, Mouse.Target.Amount)

					end
				end
				
			end
		else
			return
		end
	
	end
end


UserInputService.InputBegan:Connect(Input)

Heres the serverscript code:

Events.RemoteEvent.OnServerEvent:Connect(function(player, input, part, partvalue)
	
	local playerstats = player:WaitForChild("Stats")
	
	if input == "Eat" then
		
		if playerstats.Hunger.Value <= 100 then
			if partvalue.Value <= 100 then
				if partvalue.Value >0 then
					playerstats.Hunger.Value = playerstats.Hunger.Value + 2
					partvalue.Value = partvalue.Value - 2
				else
					if partvalue.Value <= 0 then
						part:Destroy()
					end
				end
			end
			
		else
			return 
			
		end
		
	end
	
	
	
	
end)

The object:

image

1 Like

Not enough to work with here … even if I try to make a quick program to test anything.

The part the player hovers over to fire the remote is already placed in the workspace beforehand.

I tried doing part.Amount.Value and it gave me the same error:

Also show the line its erroring on

It only returns an error.

Heres the error:

Heres the line: local PartValue = part:FindFirstChild("Amount")

heres the full code(serverscript):

Events.RemoteEvent.OnServerEvent:Connect(function(player, input, part)
	
	local PartValue = part:FindFirstChild("Amount")
	
	local playerstats = player:WaitForChild("Stats")
	
	if input == "Eat" then
		
		if playerstats.Hunger.Value <= 100 then
			if PartValue.Value <= 100 then
				if PartValue.Value >0 then
					playerstats.Hunger.Value = playerstats.Hunger.Value + 2
					PartValue.Value = PartValue.Value - 2
				else
					if PartValue.Value <= 0 then
						part:Destroy()
					end
				end
			end
			
		else
			return 
			
		end
		
	end
end)

You didnt answer my question of what part prints

1 Like

Nvm i got the reason. Set mouse.target to a variable then send the variable over, because, when you call mouse.target again if its not on the fruit wven if it was on the fruit originally it wont send the part

Events.RemoteEvent:FireServer(“Eat”, Mouse.Target, Mouse.Target.Amount.Value)

That isnt the fix because at server side he does amount.Value the problem is that the target switches. People need to stop passing mouse.Target into stuff

(0.01 seconds response, I seen that)

Like I said not enough to work with here … gl

Just means that ‘Mouse.Target’ is nil.

You cannot send objects through a RemoteEvent. Try using a RemoteFunction or send raw values.

1 Like