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)