Food stuff uhhh yeah

making remote event fire

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EatFood = ReplicatedStorage.RemoteEvents:WaitForChild("EatFood")
local Food = script.Parent.Parent

Food.Activated:Connect(function()
	EatFood:FireServer(Food)
	print("food event fired")
end)

what happens when remote event is fired (server script)

EatFood.OnServerEvent:Connect(function(player, Food)
		local hunger = player.NonPvPStats:WaitForChild("Hunger")
		hunger.Value = hunger.Value + 10
		Food:Destroy()
	end)

the problem i got is that they the food.activated dosnt work because it dosnt print anything
and i dont think the other script works to idk this is to complex for me

I believe the “Food” tool is being deleted before the local script gets the chance to print, so try adding a wait() before Food:Destroy().

EatFood.OnServerEvent:Connect(function(player, Food)
		local hunger = player.NonPvPStats:WaitForChild("Hunger")
		hunger.Value = hunger.Value + 10
        wait()
		Food:Destroy()
	end)
2 Likes

i added it but it makes no diffrence my hunger dosnt go up still

You shouldn’t do this. Exploiters can just fire the remote and give themselves infinite stats.

o fr wat should i do for an alternative

Here’s something to prevent exploiters from taking advantage of this.

EatFood.OnServerEvent:Connect(function(player, Food)
	If Food then
		local hunger = player.NonPvPStats:WaitForChild("Hunger")
		hunger.Value = hunger.Value + 10
		Food:Destroy()
	end
end)

By adding an if statement, it will check if “Food” still exists, if it does then it’ll add the stats then delete the item.

2 Likes

ah thanks for the tip always knew if statements were cool
but my my hunger doesn’t go up and it doesn’t destroy itself so the whole script not work :frowning_face:
been watching anime for 2 hours straight

is “Food” a tool (in first script)? and you’re using .OnServerEvent inside a serverscript and :FireServer() inside a localscript, right? if so where are you storing your serverscript?

yes food is a tool in the first script im just testing it out to see if it works once it does ill change it to the proper food name like hotdog or smth.
my server script is inside of serverscript service also its inside of a bigger stat script thing that i cut out cuz i thought it be kinda hard to read

local datastoreservice = game:GetService("DataStoreService")
local NonPvPStatsData = datastoreservice:GetDataStore("NonPvPStatsData")
local replicatedStorage = game:GetService("ReplicatedStorage")
local EatFood = replicatedStorage.RemoteEvents:WaitForChild("EatFood")

game.Players.PlayerAdded:Connect(function(player)
	local playerId = "Player_"..player.UserId

	local NonPvPStats = Instance.new("Folder")
	NonPvPStats.Name = "NonPvPStats"
	NonPvPStats.Parent = player
	
	local Hunger = Instance.new("IntValue")
	Hunger.Name = "Hunger"
	Hunger.Parent = NonPvPStats
	
	local Rank = Instance.new("IntValue")
	Rank.Name = "Rank"
	Rank.Parent = NonPvPStats
	
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = NonPvPStats

	EatFood.OnServerEvent:Connect(function(player, Food)
		if Food then
		local hunger = player.NonPvPStats:WaitForChild("Hunger")
		hunger.Value = hunger.Value + 10
		wait()
		Food:Destroy()
		end
	end)
	
	local data 
	local success, errormsg = pcall(function()
		data = NonPvPStatsData:GetAsync(playerId)
	end)

	if success then
		if data then
			Rank.Value = data.Rank
			Money.Value = data.Money
			Hunger.Value = 360
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerId = "Player_"..player.UserId
	local success, errormsg = pcall(function()

		local data = {
			Money = player.NonPvPStats.Money.Value;
			Rank = player.NonPvPStats.Rank.Value;
			Hunger = player.NonPvPStats.Hunger.Value;
		}

		NonPvPStatsData:SetAsync(playerId, data)
	end)
	if success then
		print("NonPvPData Saved")
	else
		print("Failed to save NonPvPData")
		warn(errormsg)
	end
end)

server script

local script

Can you seperate .OnServerEvent from .PlayerAdded event? Since first argument of .OnServerEvent is player you don’t need to put it inside the .PlayerAdded event.

like thisll

Yeah, that’s what i meant. Can you try this way?

still dont work

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EatFood = ReplicatedStorage.RemoteEvents:WaitForChild("EatFood")
local Food = script.Parent.Parent

Food.Activated:Connect(function()
	EatFood:FireServer(Food)
	print("food event fired")
end)

dosnt even print the thing

Weird, that worked for me. My scripts are:

Server script inside the ServerScriptService

local EatFoodEvent = game.ReplicatedStorage.EatFood

game.Players.PlayerAdded:Connect(function(player)
	local NonPvPStats = Instance.new("Folder")
	NonPvPStats.Name = "NonPvPStats"
	NonPvPStats.Parent = player

	local Hunger = Instance.new("IntValue")
	Hunger.Name = "Hunger"
	Hunger.Parent = NonPvPStats
end)

EatFoodEvent.OnServerEvent:Connect(function(player, Food)
	local NonPvPStats = player:WaitForChild("NonPvPStats")
	local Hunger = NonPvPStats.Hunger
	
	Hunger.Value += 10
	Food:Destroy()
	print("Worked1")
end)

Local script inside the Tool Handle

local tool = script.Parent.Parent
local EatFoodEvent = game.ReplicatedStorage:WaitForChild("EatFood")

tool.Activated:Connect(function()
	EatFoodEvent:FireServer(tool)
	print("Worked2")
end)


image

@SomeDumbNinja @Roastdbacon

This is actually not a great way to do it, as an exploiter could delete anything they wanted, such as another player’s character, or the entire map, e.g

-- this will delete the entire game map + player characters
for _,obj in pairs(workspace:GetChildren() do 
    EatFood:FireServer(obj) 
end

You could add these following if statements to secure it though. (it might make the if statement a bit longer)

-- Checks the following:
-- Does the food exist?
-- Is it a tool?
-- Is it from the player backpack/character?

if (Food) and (Food:IsA("Tool") and ( (Food:IsDescendantOf(player.Backpack)) or Food:IsDescendantOf(player.Character))  ) then
    -- code
end

why does my food give me full hunger when i made it give 10
https://gyazo.com/2e8d495c5dad6060c43254dd56c66166

local datastoreservice = game:GetService("DataStoreService")
local NonPvPStatsData = datastoreservice:GetDataStore("NonPvPStatsData")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EatFoodEvent = ReplicatedStorage.RemoteEvents:WaitForChild("EatFood")

game.Players.PlayerAdded:Connect(function(player)
	local playerId = "Player_"..player.UserId

	local NonPvPStats = Instance.new("Folder")
	NonPvPStats.Name = "NonPvPStats"
	NonPvPStats.Parent = player
	
	local Hunger = Instance.new("IntValue")
	Hunger.Name = "Hunger"
	Hunger.Value = 360
	Hunger.Parent = NonPvPStats
	
	local Rank = Instance.new("IntValue")
	Rank.Name = "Rank"
	Rank.Parent = NonPvPStats
	
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = NonPvPStats
	
	local data 
	local success, errormsg = pcall(function()
		data = NonPvPStatsData:GetAsync(playerId)
	end)

	if success then
		if data then
			Rank.Value = data.Rank
			Money.Value = data.Money
			Hunger.Value = data.Hunger
		end
	end
end)

EatFoodEvent.OnServerEvent:Connect(function(player, Food)
	if Food and Food:IsA("Tool") and Food:IsDescendantOf(player.Backpack) or Food:IsDescendantOf(player.Character)then
	local NonPvPStats = player:WaitForChild("NonPvPStats")
	local Hunger = NonPvPStats.Hunger
	
	Hunger.Value += 10
	if Hunger.Value >= 360 then
		Hunger.Value = 360
	end
	
	wait()
	Food:Destroy()
	print("Worked1")
	end
end)

Because you’re changing Hunger.Value to 0 on client so server still seeing it as 359. Change the Hunger.Value on server and see if it works.

oooo lol i always forgot about filter enable tahnks

do you know how to use dictonaries for values i tried this but it failed

EatFoodEvent.OnServerEvent:Connect(function(player, Food)
	if Food and Food:IsA("Tool") and Food:IsDescendantOf(player.Backpack) 
		or Food:IsDescendantOf(player.Character)then

		local NonPvPStats = player:WaitForChild("NonPvPStats")
		local Hunger = NonPvPStats.Hunger
		
		local FoodDictonary = {
			Burger = 10,
			Fries = 5,
			Hotdog = 15,
		}

		Hunger.Value += FoodDictonary.Food 
		
		if Hunger.Value >= 360 then
			Hunger.Value = 360
		end

		wait()
		Food:Destroy()
		print("Worked1")
	end
end)
ServerScriptService.Food:17: attempt to perform arithmetic (add) on number and nil