Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this script resetting every time a player's tool is dropped?

Ok so this script is a local script within a tool, aka an apple. This works perfectly fine with my hunger and thirst, and it has 3 uses. The one problem is if the player drops the tool then the amounts of uses gets reset back to 3. With this logic, the apple can be used an unlimited amount of time and is useless. How can I make this script retain the amount of uses in the tool even though it gets dropped by the player?

local tool = script.Parent
local player = game.Players.LocalPlayer

local HungerValue = player:WaitForChild("stats").Hunger
local ThirstValue = player:WaitForChild("stats").Thirst

amount = 3

tool.Activated:Connect(function()
		amount = amount - 1
		wait(2)
		ThirstValue.Value = ThirstValue.Value + 20
		HungerValue.Value = HungerValue.Value + 30
		player.Character.Humanoid.Health = player.Character.Humanoid.Health + 2.5
		if HungerValue.Value > 100 then
			HungerValue.Value = 100
		end
		if ThirstValue.Value > 100 then
			ThirstValue.Value = 100
		end
		if amount == 0 then
			tool:Destroy()
		end
		end)

Example: The player takes two bites of the apple and drops it. Instead of only having 1 bite left, it gets reset back to 3 and its super annoying.

1 answer

✓ Accepted answer

There are multiple ways to achieve this.

Option 1

Disable tool dropping so it doesn't happen at all

Option 2

Store the local script outside the tool

Kind of tricky to do, but will solve the problem because it'll only run the script once

Option 3

Use a ModuleScript that keeps track of the amount (or global variables)

By keeping a module, each client will have their own amount. The amount will save throughout the client even upon multiple runs. I wish I could explain it better. Here are some links that could suit you.

https://devforum.roblox.com/t/how-do-i-use-modulescripts/237334/2 https://developer.roblox.com/api-reference/class/ModuleScript

https://developer.roblox.com/articles/Variables#global-variables

Option 4

Use an IntValue to keep track of its uses

This will prevent the player from dropping it to another person giving that person x uses as well (you could also do it with a ModuleScript, you'd just need to do it on the server).

You would do server checks to subtract of its uses and make it useless once it is used x amount times (assuming your game is FE).

Good luck!

1 comment

0

Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.