Healing Script Issue - Maybe outdated scripting

Could someone please clarify why this basic healing code does not work?

I’m new to scripting and have been looking at different examples. This particular code is from a video made 3 years ago. I also have a Roblox book, copyright 2019, that has a very similar script layout, and it doesn’t work either. I understand that in Rolbox coding styles become outdated and new formats are implemented. Can you clarify:

  1. What part of this code is no longer the current scripting style and
  2. What the current scripting should be

The game setup is a single part placed on the ground with a Script attached. The goal is to make the part heal the player 25 points when touched. Upon starting the game in Roblox Studio I go into my character’s Humanoid properties and change my Health to 10. I then walk on the part and it should heal me +25. This script no longer functions as it once did for others in the past. The script is:

local debounce = false

game.Workspace.Part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
if not debounce then
debounce = true
while hit.Parent.Humanoid.Health < 100 do
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health + 25
wait(2)
end
wait(2)
debounce = false
end
end
end)

Use the wiki link and this example code.

local part = game.workspace:WaitForChild('Part') -- or a dot
part.Touched:Connect(function(hit)
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if player then
        -- le code
    end
end)

What’s not working is the touched event in general. I think it’s deprecated not sure. Other than that I think you can use the code. You could also define the variables better didn’t mention before.

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()

then rest of the code

I wouldn’t worry about scripting styles, they seem to change from programmer to programmer and develop over time from experience. There isn’t a correct universal scripting style that changes over time. What contributes to an outdated script is the usage of depreciated functions (ex in roblox. using :remove())

I think the issue you’re experiencing is due to a flawed test design. Roblox operates on a client-server model and because of filteringEnabled, new data on client often doesn’t replicate back to server (some exceptions exist). So when you’re changing the health value, the server still believes you’re at full health. Since the script you run is server-sided it will not pass the statement checking if the players health is below 100. You can fix this test by changing health on server, just toggle the client/server button in the test window in studio.

1 Like

The main issue seems to be the situation mentioned by SilvRHat. I was changing the character’s health while on the Client side of playing Roblox Studio. I went under the Test tab and clicked the button Current:Client, switching the studio screen to Current:Server. I changed the character’s health (while now under Current:Server) and when I touched the healing block it worked as hoped! I have never been introduced to the Current:Client/Server button. I’ll have to research it further to see when to use each option.