Problem with heal to max health script

I want to heal the player to max health when they choose a certain dialog.
However, If the player had damage, it would heal to max health then deduct the health again.
A video example:
robloxapp-20200809-2258436.wmv (4.7 MB)

I do have another script which deducts health but it does not conflict with it according to the output.

The script:

game.Workspace.Stranger.Head.Dialog.DialogChoiceSelected:Connect(function(player,choice)
	if choice.Name == "Heal" then
		print("Player wants to heal")
		wait(1)
		for i, player in ipairs(game.Players:GetPlayers()) do
			if player.Character then
				local plr = player.Character:FindFirstChild("Humanoid")
				if plr then
					plr.Health = 100
					print("Player Healed")
				end
			end
		end
	end
end)

The other script I was talking about but has no chance of conflicting with it

game.Workspace.RichStranger.Head.Dialog.DialogChoiceSelected:Connect(function(player,choice)
	if choice.Name == "Yes" then
		print("player got tricked")
		wait(10)
		for i, player in ipairs(game.Players:GetPlayers()) do
			if player.Character then
				local plr = player.Character:FindFirstChild("Humanoid")
				if plr then
					if plr.Health > 20 then
						plr.Health = 20
						print("Player Health Deducted")
					end
					
				end
			end
		end
	end
end)

So what went wrong with the first script? What do I need to add to ensure that it does not happen?

Is your script printing everything that it needs to? I mean that have you tested the script and all options were printed

Yes it is printing everything it needs to

Edit: Including the output
22:58:43.964 - SoundService::getSampleRate: system value = 0000025EE23A9048, enabled value = 1

22:58:43.965 - SoundService::getSampleRate: result = 48000

22:58:43.965 - Video recording started

Player wants to heal

Player Healed

Player wants to heal

Player Healed

Player wants to heal

Player Healed

22:59:27.985 - Video recording stopped

Are you trying to heal every player or just the player that chose the certain dialog and jsut to make sure, is this in a server script or local script?

Just the layer that chose the dialog

Try adding a script at the position mention above -

if plr then
  if plr.Health <= 100 then 
	   plr.Health = 100
    end
end

I’m not a 100% sure this will work

Try using this. from what I believe, you are looping through each player in the game and changing their health. You only want to use that player. Because of that, you don’t need the for loop because I believe the player argument is an object so you can just do player.Character. Try that and see what happens. [Make sure it’s a server script and not local]

game.Workspace.Stranger.Head.Dialog.DialogChoiceSelected:Connect(function(player,choice)
    if choice.Name == "Heal" then
        print("[".. player.Name.. "] wants to heal")
        wait(1)
        if player.Character then
            local plr = player.Character:FindFirstChild("Humanoid")
            if plr then
                plr.Health = plr.MaxHealth
                print("[".. player.Name.. "] was healed - ".. plr.Health)
            end
        end
    end
end)

Where should I put this under? It does not work under starter character scripts

Sorry, but it was still the same result

1 Like

What did the output say or was there any output at all?

Edit:
Try putting that script inside ServerScriptService in a normal script (not local or module) and see what happens.

I think this script should be within the stranger itself, then you don’t need multiple scripts and it will be easier to edit, however, this script should work anywhere serverside (not under the player itself).

Nothing was printed in the output

If nothing is output, then it probably never ran the code and there’s a likely problem earlier on.

If you haven’t then try putting it into ServerScriptService or do what @tinysam2004 and put it within the Stranger itself.

That is correct, I apologize, I haven’t used dialog in my code before. This should be under StarterPlayerScripts then. Make sure you make it a local script.

I think DialogChoiceSelected are not meant to be run under scripts

The following from roblox developer

DialogChoiceSelected

Event of:

Dialog

Description:

Fired when a player chooses something to say, through a Dialog instance.

This event is client-side only and will not fire on the server. It should be connected to in either a LocalScript or a ModuleScript required by a LocalScript .

You will need to fire an event to the server script. Example code could be something such as this.

-- Make sure to create a RemoteEvent in ReplicatedStorage: I named the event "HealPlayer" in this example

--- Local Script
game.Workspace.Stranger.Head.Dialog.DialogChoiceSelected:Connect(function(player,choice)
    if choice.Name == "Heal" then
        print("[".. player.Name.. "] wants to heal")
        game.ReplicatedStorage.HealPlayer:FireServer()
    end
end)

--- Server Script
game.ReplicatedStorage.HealPlayer.OnServerEvent:Connect(function(player)
    if player.Character then
        local humanoid = player.Character:WaitForChild("Humanoid")
        if humanoid then
            humanoid.Health = humanoid.MaxHealth
            print("[".. player.Name.. "] was healed - ".. humanoid.Health)
        end
    end
end
1 Like

Beware, this code will allow exploiters to heal them whenever they want since the client decides when it wants healing. You will likely want to find a way for the server to detect that the player has performed the necessary actions before they can heal. Always remember: Any clientside code can be read and changed by the client.

Edit: Actually, you can get all players currently using the dialog with Dialog:GetCurrentPlayers() command.

Right, but right now solving the issue is what needed to be done. The best thing would be to use a Region3 or magnitude to find the distance between the player and the Stranger because you can still exploit .Touched events I believe (correct me if I’m wrong).

Actually, I just found there’s a command to get all players using the dialog, so that would be the best method. I recommend using Dialog:GetCurrentPlayers().