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
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.
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
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)
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.
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
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)
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