Need Help Making a Simple Script(s)

ok so when an npc’s health gets lower than 50, a proximity prompt appears on the npc and if you activate it, all the npc’s accessories get transported to you and then the npc is destroyed. If you decide to help me, please include a screenshot of your explorer. this is the scripts i made and don’t work:

(keep in mind that even if the npc health is below 50, the proximity prompt doesn’t show up so i haven’t tested the script inside of the prompt yet.)

script in server script service:

local dummy = game.Workspace.Dummy

if dummy.Humanoid.Health <= 50 then 
dummy.ProximityPrompt.Enabled = true
end

this script works if i automatically set the npc health to 50, but not if it’s at 100 and i use my sword to make it below 50.

this is the script inside of the proximity prompt (can u help me make it so that it takes everything inside the dummy that’s an accessory)

script.Parent.Triggered:Connect(function(plr)
script.Parent.Parent.Horns:Clone().Parent = plr.Character
wait(1)
script.Parent.Parent:Destroy()
end

I think that last script you showed is invalid, because there should be a “)” after the end keyword in the last word. If you’re trying to check if the health is under 50, I recommend using :GetPropertyChangedSignal() function event. Whenever it fires, check if it’s under or equal to 50. If so, add the proximity prompt, and check when it’s activated. you can then add the items there. Here’s an example if you don’t understand.

local dummy = game.Workspace.Dummy

dummy.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    if dummy.Humanoid.Health <= 50 then
        -- add proximity prompt and triggered event
        dummy.ProximityPrompt.Enabled = true

        dummy.ProximityPrompt.Triggered:Connect(function(plr)
            -- add items here.
        end)
    end 
end)

This script should be in serverscriptservice, or workspace. Also, make sure the proximityprompt is inside of the dummy. I’m kind of confused with the fact that you just used a simple if statement to check if the health was less than 50, unless you only need to check once.

and this is all in one script? under server script service?

1 Like

and what do you mean by add items here? i know i need to put soemthing that sorts through the children and sees which ones are accessories

Do this here, when the proximity prompt is triggered. If it doesn’t work, tell me.

1 Like

you just put

if dummy.Humanoid.Health <= 50 then 
dummy.ProximityPrompt.Enabled = true
end

you should put a while true do so that it continuously checks the NPC’s health

while true do
wait() -- make sure you put the "wait()" there or else it will crash the game
if dummy.Humanoid.Health <= 50 then 
dummy.ProximityPrompt.Enabled = true
end
end

very simple way to fix it

I wouldn’t use this method, because it unnecessarily wastes processing power. Simply use an event, and it will only run the code when it changes.

1 Like

ok i will see if this works but i don’t have my computer currently so i will let you know later if this is the solution

ok i will when i get back to my currently and then i will update you

1 Like

Yeah I know it may waste power but it probably won’t have a very significant effect on preformance.

Also I’m still kinda new to programming so it was the only real solution I could think of.

2 Likes

this would be under server script service, yes?

1 Like

it does work, but then how do i do the script where the proximity prompt is enabled when the humanoid’s health <= 50?

it doesnt work, it only works if i set the starting health for the npc to 50

You already have. Use the script I showed you. If you want to disable it when the dummies’ health goes above 50, just use else and disable it. I’m 99% percent sure this script should work.

local dummy = game.Workspace.Dummy

dummy.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    if dummy.Humanoid.Health <= 50 then
        -- add proximity prompt and triggered event
        dummy.ProximityPrompt.Enabled = true

        dummy.ProximityPrompt.Triggered:Connect(function(plr)
            -- add items here.
        end)
    end 
end)

You may have to disconnect previous events, though.

1 Like

Does the health actually change? or is it just set at start, and is stagnant?

thank you! it now works!

(char limit)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.