Hello, I am trying to limit the amount of drinks a player can hold. I currently have 3 scripts that relate to this, however only one is having issues which I will be sharing here. One script is in the ServerScriptService, and its only purpose is to put an IntValue(drink count number) into players upon joining. The second script controls the actual drinking (animation, sound, increase food stat, and subtract 1 from drink count when drank) and is a local script. Lastly the problem child, this script controls the Proximity Prompt, checking if the player has the max amount of drinks and giving the player the drink.
The script works almost as intended. I can pick up 8 drinks, which is the max I have set, and if I try to pick up more than 8 it stays parented to the workspace which is what I want. However, if I drink one and the IntValue count goes to 7 or even below it still wont let me pick up another drink. I have tried setting out 9, drinking 8 of them each individually and the trying to pick up another but it still spits out the “max drinks” print.
I would appreciate any help
here is the script:
local tool = script.Parent.Parent
local prompt = script.Parent.Parent.ProximityPrompt
local touch = script.Parent:WaitForChild(“TouchInterest”)
touch:Destroy()
prompt.Triggered:Connect(function(Player)
local maxTool = 8
local toolCount = Player:FindFirstChild("WaterCount")
if toolCount.Value >= maxTool then
tool.Parent = workspace
script.Parent.Anchored = true
print("Max Water")
elseif toolCount.Value < maxTool then
script.Parent.Anchored = false
tool.Parent = Player.Backpack
prompt:Destroy()
toolCount.Value = toolCount.Value + 1
end
I just tried that, its adding and subtracting correctly. I called the subtract after I deleted the drink so i fixed it to subtract before deleting the drink but the problem still persists for some reason.
The script you provided is not the issue, instead it lies within this:
Changes in a local script don’t replicate to the server, on the server it stays the same amount therefore causing the issue. You need to use a RemoteEvent and tell the server that a drink has been used, then subtract the drink amount on the server
The script i posted is in the Handle of the drink, and the local script that controls the actual drinking and subtraction is located in the drink itself not in any parts. The script that handles the intvalue is in the server script service.
here are those scripts:
Local script for actual drinking - script works as intended
local Bottle = script.Parent
local slurp = Bottle.slurp
local anim = Bottle.DrinkAnim
local Drank = false
script.Parent.Activated:Connect(function()
if Drank then
return
end
local LocalPlayer = game:GetService("Players").LocalPlayer
local toolCount = LocalPlayer:FindFirstChild("WaterCount")
Drank = true
local char = Bottle.Parent
local hum = LocalPlayer.Character.Humanoid
local animtrack = hum:LoadAnimation(anim)
animtrack:Play()
Bottle.Lid:Destroy()
slurp:Play()
wait(1)
game.ReplicatedStorage.Drink:FireServer() -- related to an event that controls food bar gui
print("FIRED")
toolCount.Value = toolCount.Value - 1
print(toolCount.Value)
script.Parent:Destroy()
end)
script that controls the IntValue adding upon joining - located in ServerScriptStorage
game.Players.PlayerAdded:Connect(function(Player)
ahh okay that makes sense. all along i thought that one was working as intended but as it turns out it was the problem lol
I will implement this and give an update
Update: that was the solution! I added another event to the local script and added a script into the serverscriptstorage that changes the value upon drinking and now it is working beautiful! thank you all for your help I really appreciate it