Problem with a health button

Hey,
So I’m making a health button, when you click, you have more health.
So for that, I use a Remote Event, when you click to a button, a Remote Event is activate and the event is recieve.

Here is the script:

 game.ReplicatedStorage.BuyButtonValue.OnServerEvent:Connect(function(plr)
 game.Workspace:WaitForChild(plr.Name).Humanoid.Health = 
  game.Workspace:WaitForChild(plr.Name).Humanoid.Health + 30
 plr.Settings.Medicine.Value = plr.Settings.Medicine.Value - 1
 end)

Here is the local script:

  local plr = game.Players.LocalPlayer
  script.Parent.MouseButton1Click:Connect(function()
  if plr.Settings.Medicine.Value > 0 then
 game.ReplicatedStorage.BuyButtonValue:FireServer()
 end

 end)

I also have a problem, when I click on a button to buy Medicine, the Money value change to -750 and not for example 2000-750= 1250
Here is the script:

    game.ReplicatedStorage.BuyMoneyValue.OnServerEvent:Connect(function(plr)
plr.Settings.Medicine.Value = plr.Settings.Medicine.Value + 1
plr.Settings:WaitForChild("Money").Value = plr.Settings.Money.Value - 750
  end)

Here is the local script:

       local plr = game.Players.LocalPlayer
  script.Parent.MouseButton1Click:Connect(function()
if plr.Settings.Money.Value >= 750 then
game.ReplicatedStorage.BuyMoneyValue:FireServer()
end
 end)

Helppppp :smiley:

2 Likes

Are there any errors in the output relating to the script?

Yeah yeah sorry,

“ServerScriptService.Script, Line2” Attempt to index a nil value

You’re indexing a nil value, so I guess that ‘Medicine’ or something doesn’t exist in the directory you’re indexing. Also I suggest checking if player has enough money on the server, not in the local script becuase this is not safe.

I’m sorry, I didn’t put the right script on the topic :expressionless:

1 Like

Seems to work fine except for the money going from 750 to -750 (might be my fault trying to recreate the script in studio)
Assuming this might be one of the issues:

game.ReplicatedStorage.BuyMoneyValue:FireServer()

Try doing this instead:
game.ReplicatedStorage.BuyMoneyValue:FireServer(plr)

EDIT: Nevermind, didn’t notice you changed the script.

I just noticed that you edited the code so my previous answer is wrong, so you have to do FindFirstChild(plr.Name) instead of FindFirstChild(plr)

I also have this problem, when I have 1000 money, I click and it appear -750, how to change that ??

Did I solve your problem? Now please give us the complete code because the one you provided does not contain the part where money is being subtracted from the players money count.

1 Like

Also, what is this line here for? You already have a change health line:
game.Workspace:FindFirstChild(plr).Humanoid.Health + 30

but yeah, like @Kacper said, with the new script, you’re not showing us where you’re subtracting money.

1 Like

This is the whole line

game.Workspace:FindFirstChild(plr).Humanoid.Health = game.Workspace:FindFirstChild(plr).Humanoid.Health + 30
1 Like

Ah okay, I see. The script is weirdly formatted (whether that’s just me or the DevForum’s code formatting), so I apologise.

It is indeed OP’s fault :upside_down_face:

1 Like

Can you not edit the original post but add edits as a new post under the topic?

I edit with the two problems. With Money not correcly and the Health not giving Health to player

It’s happening because you have to check if player has enough money on the server, not in the local script, because of the connection latency you will have problems like that.

What I have to do to change that ?

On the server:

game.ReplicatedStorage.BuyMoneyValue.OnServerEvent:Connect(function(plr)
    local money = plr.Settings:WaitForChild("Money");
    if money.Value >= 750 then
        plr.Settings.Medicine.Value = plr.Settings.Medicine.Value + 1
        money.Value = money.Value - 750
    end
end)

Local script:

local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.BuyMoneyValue:FireServer()
end)

And you should do the same thing with your script where you use medicine to get heatlh.

Try your script with print. Print only the 1

game.ReplicatedStorage.BuyMoneyValue.OnServerEvent:Connect(function(plr)
print(1)
local money = plr.Settings:WaitForChild(“Money”)
if money.Value >= 750 then
print(2)
plr.Settings.Medicine.Value = plr.Settings.Medicine.Value + 1
print(3)
money.Value = money.Value - 750
print(4)
end
end)

Put print(‘test’) after

local money = plr.Settings:WaitForChild("Money")