I want to make my NPC drop coins when you click him but I cant figure out how. Can you help?
COMMUNITY QUESTION
NPC dropping coins when you click it, help?
1 answer
I would probably start by making a part inside the NPC model thats about the size of the NPC (A hitbox), turning off CanCollide and making it transparent, then adding a weldconstraint in the model and welding the part to the humanoidrootpart so it sticks to the NPC without it falling out
After that i'd add a clickdetector inside the hitbox part and also a server script.
Add coin model in serverstorage
Inside the coin model add a clickdetector and add a server script inside the coin
Script inside hitbox part:
local coin = game.ServerStorage:WaitForChild("Coin") -- You can change "Coin" to whatever the coin model is called
script.Parent.ClickDetector.MouseClick:Connect(function()
-- when the NPC's hitbox is clicked:
print("Clicked")
-- cloning the coin model and making it spawn by the NPC
local coinclone = coin:Clone()
coinclone.Parent = workspace
coinclone.Anchored = true
coinclone.Position = Vector3.new(script.Parent.Parent.HumanoidRootPart.X, script.Parent.Parent.HumanoidRootPart.Y, script.Parent.Parent.HumanoidRootPart.Z)
coinclone.CanCollide = false
end)
Script inside coin code:
script.Parent.ClickDetector.MouseClick:Connect(function(player)
-- Add whatever you want to happen to the coin when you click it, could add some sounds, particles etc.
script.Parent:Destroy()
end)
I havent tested this out but it should work, let me know if you get any errors or need more help. Other than that, good luck!
This discussion is preserved as it was. New answers, comments, and votes are closed.
