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

Character will not receive extra maxhealth after touching button?

I have a script that when the parent (button) is touched and the player has more than 5 Quarcks (the playstats currency name) then it takes away 5 Quarcks and gives the player an extra 10 MaxHealth for only one life, the brick turns black for a second before returning to red. The script is as follows:

local brick = script.Parent
local maxheal = 10
local price = 5
local player = game.Players.LocalPlayer
local stats = player:WaitForChild("leaderstats")

local function onPartTouch(otherPart)
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if stats.Quarcks.Value == price then
	stats.Quarcks.Value = stats.Quarcks.Value - price
	if humanoid then
		local currentHealth = humanoid.MaxHealth
		local newHealth = currentHealth + maxheal
		humanoid.Health = newHealth
		brick.BrickColor = BrickColor.new("Black")
		wait(1)
		brick.BrickColor= BrickColor.new("Really Red")
	end
end
brick.Touched:Connect(onPartTouch)

I'm not sure what is going wrong as when I try to touch the brick, nothing happens. The model is as follows: Model> Humanoid, Head - Head> Script.

1 answer

✓ Accepted answer

The problem is in "player = game.Players.LocalPlayer". If it is not a local script this method won't work. Instead you can use (I assume that the button is not in the character)

local player

-- local function onPartTouch(otherPart)
	--etc
	player = game.Players:GetPlayerFromCharacter(partParent)
-- etc

1 comment

0

This worked, thank you. I found out I mucked a bit of my script a bit and put some lines in the wrong place

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