Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
zValerian 108
5 years ago
Edited 5 years ago

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)




0
Use a regular script then use the Touched function. Put the script in each water part. herrtt 387 — 5y
0
I would but it does not work, I tested it zValerian 108 — 5y
0
what script would you put in a block, maybe I missed something zValerian 108 — 5y
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" yellp1 193 — 5y
View all comments (2 more)
0
here is what I got : zValerian 108 — 5y
0
check my post I updated it zValerian 108 — 5y

1 answer

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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

0
does this make it so that it works with all objects called water? zValerian 108 — 5y
0
No, Only the first on the workspace called water(this is what FindFirstChild does) Leamir 3138 — 5y
0
If you want I can make it work with all objects called water Leamir 3138 — 5y
0
You can also change the block to script.Parent and add this script to all water blocks... Leamir 3138 — 5y
View all comments (4 more)
0
Please make it work with all objects called water <3! zValerian 108 — 5y
0
Done Leamir 3138 — 5y
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? zValerian 108 — 5y
0
Look to the name of the block, it must be Water (Case Sensitive) Leamir 3138 — 5y
Ad

Answer this question