Change Player Value on part touched

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.

image

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)

That should work.

1 Like

It isnt working. It also dosent give me an error message.

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)

That error is because it didn’t find the player as you didnt add a check to check that the player exists!

When I try this nothing happens.

Mind running a print(player:GetChildren()) for us?

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)

Script^
Output Bellow
image

Nothing happens, nothing in the output as well.

Are you using a NumberValue/IntValue?

I am using NumberValues.

[Stupid not long enough thing]

Where is the script located, inside of the part you are touching?

Yes. The script is inside the part I am touching.

I ran a test in an empty game and the original code worked as intended. Are you sure there isn’t another script influencing the value of Water?

Try using WaitForChild instead of FindFirstChild

Like this:

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

Just this script which makes the values.

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)

Just tried it, It doesn’t work.

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)
2 Likes