I am trying to make a script so when you touch a block it changes a value inside of you. Inside the player there is a folder with the value Water in it. What I want is so when you touch a part it sets your water value to 100. I have a script below that prints the value but doesn’t change my value.
local Players = game:GetService("Players")
local part = script.Parent
local function onTouched(part)
local player = Players:GetPlayerFromCharacter(part.Parent)
if not player then return end
print(player.Name .. " pressed me!")
player.Stats.Water.Value = 100
end
part.Touched:Connect(onTouched)
local part = script.Parent
part.Touched:Connect(function(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end
player.Stats.Water.Value = 100
end)
Ah, most likely because “Stats” or “Water” doesn’t exist. Try:
local part = script.Parent
part.Touched:Connect(function(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end
player:FindFirstChild("Stats", true).Water.Value = 100
end)
When I try that nothing happens with no error messages.
When I try this code I get that error message.
local part = script.Parent
part.Touched:Connect(function(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
Player:FindFirstChild("Stats", true).Water.Value = 100
end)
I solved something similar to this recently (here)
local Players = game:GetService("Players")
local part = script.Parent
part.Touched:Connect(function(hit)
if not (Players:GetPlayerFromCharacter(hit.Parent)) then return end
local Player = Players:GetPlayerFromCharacter(hit.Parent)
Player:WaitForChild("Stats").Water.Value = 100 -- Maybe try waiting? FindFirstChild should work though
end)
local part = script.Parent
part.Touched:Connect(function(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not Player then return end
print(Player:GetChildren())
Player:FindFirstChild("Stats", true).Water.Value = 100
end)
local part = script.Parent
part.Touched:Connect(function(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not Player then return end
Player:WaitForChild("Stats", true).Water.Value = 100
end)
Also your using Player with a capital P not a lowercase p
game.Players.PlayerAdded:Connect(function (player)
local f = Instance.new("Folder")
f.Parent = player
f.Name = "Stats"
local t = Instance.new("NumberValue")
t.Parent = f
t.Name = "Thirst"
t.Value = 100
local h = Instance.new("NumberValue")
h.Parent = f
h.Name = "Hunger"
h.Value = 100
local o = Instance.new("NumberValue")
o.Parent = f
o.Name = "Oxygen"
o.Value = 100
local w = Instance.new("NumberValue")
w.Parent = f
w.Name = "Water"
w.Value = 100
end)
I know the problem, the water is already at 100, and you’re setting it to 100 when touching the part.
The code I supplied should be working, try changing the value. ( I set it to 200)
local Players = game:GetService("Players")
local part = script.Parent
part.Touched:Connect(function(hit)
if not (Players:GetPlayerFromCharacter(hit.Parent)) then return end
local Player = Players:GetPlayerFromCharacter(hit.Parent)
Player:WaitForChild("Stats").Water.Value = 200
end)