The first problem is caused by you not using a local variable.
When you say m = , it refers to some shared m -- one that any different call to this function will use.
That is almost never what you want, and modern coding practices say this is a very bad thing to do.
Make m local:
....
local m = Instance.new("Message", game.Workspace)
....
TL;DR: When first assigning to all variables, declare them as local (you can search on SH for lots of questions concerning how to use local)
The second thing would be a different sort of debounce. Instead of us using true and false, we'll use a dictionary:
announced[model]
will be true if model has been announced, and otherwise nil. We just need to use this instead of debounce:
announced = {}
function announce(part)
local model = part.Parent
if model and not announced[model] then
announced[model] = true
local m = Instance.new("Message", game.Workspace)
m.Text = model.Name .. " has entered the battlefield!"
wait(3)
m:Destroy()
end
end
Because of how table indexing works, we can use model (as opposed to model.Name) to capture a reference to that particular model -- when you respawn, you'll get a new one, so it will ignore the old entry.
This is a neat trick that avoids us from having to track the .Died or .CharacterAdded events or anything else -- we get it for free!
(This will accumulate garbage; it will remember every player who has ever touched it in any life, even if they're long gone, so it might be a good idea to clear out values that are no longer in the Workspace, though this shouldn't be necessary)
1 comment
There's a terrible comment -- what doesn't work? I had forgotten to include the definition of our dictionary (though that would be clear, and "It says
announcedwasn't defined" would be a MUCH better comment). Also remember to read and not blindly copy ... for instance, this doesn't have a connection line in it.