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

how can I make this local script affect all the parts with the same name?

This script makes a local players thirst bar go up when they touch water. However, if there are more then one brick with the same name, it does not work, does anyone know how to get around this and make it work?

-- Localscript
repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character
local player = game.Players.LocalPlayer
local character = player.Character
local block  = game.Workspace:FindFirstChild("Water")
wait()
local thirst = game.Players.LocalPlayer.ThirstVal  
local debounce = false

block.Touched:connect(function(p)
    if debounce then return end
    debounce = true
    local humanoid = p.Parent:FindFirstChild'Humanoid'
    if humanoid and p.Parent == character then
        thirst.Value = thirst.Value + 10
if thirst.Value > 100 then
	thirst.Value = 100
end
    end
    debounce = false
end)



This is another take on it that doesn't work...

-- Localscript
repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character
local player = game.Players.LocalPlayer
local character = player.Character
local block  = game.Workspace:GetChildren()

wait()
local thirst = game.Players.LocalPlayer.ThirstVal  
local debounce = false

player.Character.HumanoidRootPart.Touched:connect(function(p)
	if block.Name == ("Water")
	then
    if debounce then return end
    debounce = true
    local humanoid = p.Parent:FindFirstChild'Humanoid'
    if humanoid and p.Parent == character then
        thirst.Value = thirst.Value + 10
if thirst.Value > 100 then
	thirst.Value = 100
end
else
	print("a")
    end
    debounce = false
	end
end)




6 comments

0

Use a regular script then use the Touched function. Put the script in each water part.

0

I would but it does not work, I tested it

0

what script would you put in a block, maybe I missed something

0

use a local script like you are now, but instead of detecting if the water part was touched, how about trying to see if the character's "HumanoidRootPart" was touched. Then when it was, check to see if the name of the part being touched was named "Water" by doing something like this, "if p.Name == "Water" then"

0

here is what I got :

0

check my post I updated it

1 answer

I editted your script to be a Server Script, hope this helps

local block  = game.Workspace:FindFirstChild("Water")
wait()
local debounce = false

block.Touched:connect(function(p)
    if p.Parent:FindFirstChild("Humanoid") and game.Players:FindFirstChild(p.Parent.Name) then
		local character = p.Parent
		local thirst = game.Players:FindFirstChild(p.Parent.Name):FindFirstChild("ThirstVal")
	    if debounce then return end
	    debounce = true
	    local humanoid = p.Parent:FindFirstChild('Humanoid')
	    if humanoid and p.Parent == character then
	        thirst.Value = thirst.Value + 10
			if thirst.Value > 100 then
			    thirst.Value = 100
			end
	    end
	    debounce = false
	end
end)

This will work with all parts called "Water" (You can change this on the first line...)

local block = "Water"
wait()
local debounce = false
local thirst = game.Players.LocalPlayer:WaitForChild("ThirstVal")
repeat wait() until  game.Players.LocalPlayer.Character
local character = game.Players.LocalPlayer.Character

game.Players.LocalPlayer.Character.HumanoidRootPart.Touched:connect(function(p)
    if p.Name == block then
		thirst.Value = thirst.Value + 10
		if thirst.Value > 100 then
			thirst.Value = 100
		end
   end
end)

Good Luck with your games

8 comments

0

does this make it so that it works with all objects called water?

0

No, Only the first on the workspace called water(this is what FindFirstChild does)

0

If you want I can make it work with all objects called water

0

You can also change the block to script.Parent and add this script to all water blocks...

0

Please make it work with all objects called water <3!

0

Done

0

hey I tested the script and it doesn't work, the one where its supposed to work with all objects named water, it doesnt work on even one, also, if I just change the block to script.Parent, that only works in studio and not the real game... any ideas?

0

Look to the name of the block, it must be Water (Case Sensitive)

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