Using this expertly drawn diagram.
The actual code snippet
local script
bottle.Equipped:Connect(function()
local character = bottle.Parent
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.Q then
local raycastResult = workspace:Raycast(character.PrimaryPart.Position, Vector3.new(0, -1, 0) * 5, raycastParams)
if raycastResult.Material == Enum.Material.Water then
if refillDebounce == false then
refillDebounce = true
refillEvent:FireServer(bottle)
task.wait(refillCooldown)
refillDebounce = false
end
end
end
end)
end)
server script
refillEvent.OnServerEvent:Connect(function(player, bottle)
NumOfUses.Value = 2
print(NumOfUses.Value)
VisualWater()
end)
1 Like
Probably because the OnServerEvent is not exclusive to the bottle you have equipped. You may has passed is a parameter but if the changes being made inside that server function is being read and used by the other bottles, any changes made to it, they’ll acquire as well.
2 Likes
I figured that was the issue, I tried a few things to circumvent the issue, but I wasnt able to find a proper solution, I am not really sure what to do here.
1 Like
What you could do is to not have a single Value/variable being used by all the other bottles. You could have one variable specific for each bottle. Since you have passed the bottle equipped as an argument, you could just go
bottle.NumOfUses.Value = 2
That is, if the NumOfUses is the value being refilled.
1 Like
So a variable thats unique to each bottle, like an ID system of the sort? I am not sure if that could work entirely, unless i made a random ID system, because I plan to have a survival game and these bottles would be generated/cloned as players explore for loot?
Ill make sure to try this though.
1 Like
Not really an Id system. It shouldn’t be that complicated really. I don’t know what you’re using to create the bottles but considering every bottle will have different values for NumOfUses and this bottle might have that value replenished and other won’t, it’s expected you’ll have that value specific for each bottles. What are you considering though?
1 Like
Mmm, no the NumsOfUses isn’t randomized or different between bottles, they all have 2 which decreases as they are drank by one, basically just a value that lets the player drink the bottle a few times before its empty.
Really the issue is that all bottles get their NumsOfUse get replenished, regardless of being equipped, dropped, or what else, while it should only be the equipped bottle, maybe I should try to define the actual tool some other way?
Right now I define the tool like this
local Consumable = script.Parent
In both local, and server scripts, which are in the tools themselves.
1 Like
What if you had like two bottles though and you drink the other one? Wouldn’t that make one of the bottle have the NumOfUses set to 1 and the other still full would be 2? That would make it different between bottles then or maybe I’m not getting it, I just woke up, so I apologize. Because I was thinking of having just a NumOfUses Value parented to each of the tool.
Also, it’d be better if you use one script to perform this instead of having one script per bottle. I’m assuming you have one script per each tool since you’re doing script.Parent.
1 Like
When you drink one bottle while having another one, only the drank bottle has a value reduced is because tool.activated works on serverscripts just fine, while UserInputService doesnt, and im forced to use a local script + remote events
And another question, How would I make it so several tool operate and work with a single script? I’ve figured it out with NPCs but I dont understand how I would do it with tools?
Mmm, chances are ill find a work around where I dont have to use a local script and keep using tool.activated, but please do tell me abt handling tools withing a single script (which i assume is a script in serverscriptservice)
1 Like
The thing is, you’re having the Bottles spawn around the world so it’s not a good idea to just have tools scattered all around. You should put a place holder object that represents the bottle. That should be the one being generated/spawned by a single script. You could probably have it so once you’ve interacted with the bottle object, a bottle tool is gonna get replicated to your backpack. I just realized that if you’re gonna have the tool just replicated to your backpack, you don’t necessarily need to make it one script for all the tool unless you want things to really be centralized. But for the bottles that are all over your world, the script that generates them and the function that enables a character to interact with them should just be one for optimization purposes.
Oh I know to not have tools scattered around, Looting will have a system that prevents and stylizes it in a way that it prevents that. But I see.