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?
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?
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)
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).
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.
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
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().