How to check if a string has a certain text with a script?

i am making a glass bottle to pickup water and the script i used to pickup water actually works it changes the tooltip from empty to full.
the issue:
now that i want to check if the tooltip is indeed full (with a scipt)
it doesn’t work.
script in question

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr.Backpack:FindFirstChild("bottle").ToolTip == "Full" then
		script.Parent:Destroy()
	end
end)

im probably doing it wrong but i want to know how to really check if a string has a certain text
(if there is even a way to check that)
(for if you haven’t noticed yet i am a beginner)

Is the bottle located in the backpack when it happens?
Also, what are you trying to compare/check here?

if plr.Backpack:FindFirstChild("bottle").ToolTip:find("Full") then -- find the string pattern "Full" in the ToolTip
script.Parent.Touched:Connect(function(hit)
-- Is hit.Parent always a Character? Doesn't have to be. How about change it to a clickdetector? That's more efficient, because it won't run this everytime anything touches it.
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
-- I'd seperate looking for the bottle and checking the tooltip first.
	if plr.Backpack:FindFirstChild("bottle").ToolTip == "Full" then
-- You're checking if the bottle is full. If so, you'll remove script.Parent (which I assume is the water). This is the wrong way around, as you'd more likely need to check if it's empty before adding new water. If it's full, do nothing. I assume this is the reason why your code isn't working, btw.
		script.Parent:Destroy()
	end
end)

A better way to do it would be this:

script.Parent.ClickDetector.MouseClick:connect(function(plr) -- Run this when clicked
	if plr.Backpack:FindFirstChild("bottle") == nil  then -- Check if player has bottle
		warn("no bottle")								
	else
		
		if plr.Backpack["bottle"].ToolTip ~= "Full" then	-- Check if bottle is full
			warn("bottle already full")	
		else
			script.Parent:Destroy()						-- Destroy water
			plr.Backpack["bottle"].ToolTip = "Full"		-- Set ToolTip to full
		end
	end
end)

It would actually be even better if you approach this from the tool itself.

Inside a localscript, in the tool:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

script.Parent.Activated:Connect(function()
	if mouse.Target == nil or mouse.Target.Name ~= "water" then -- Check if the mouse is on valid target
		warn("Wrong target.")
	else
		if script.Parent.ToolTip == "Full" then	-- Check if full
			warn("Bottle full")
		else
			print("Succes!")
			game.ReplicatedStorage:WaitForChild("waterEvent"):FireServer(script.Parent, mouse.Target) -- Tell the server you're picking up the water.
		end
	end
end)

In a server, in ServerScriptService

local waterEvent = Instance.new("RemoteEvent", game.ReplicatedStorage) -- Create a new remote event
waterEvent.Name = "waterEvent"

waterEvent.OnServerEvent:Connect(function(Player,bottle,waterPart)
	waterPart:Destroy()
	bottle.ToolTip = "Full"
	bottle.Enabled = false
end)
1 Like

yes it has to be located in the backpack.
im trying to make a door that only opens if the bottle is full
so im checking if it is by checking if the tooltip says full instead of empty

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.