Hello developers! So basically in a tool if a players mouse clicks on a player on a certain team, I want it to change text (I can change the text). No errors, and I used print statements and everything printed exepct for ‘hooray’ and ‘re’ Here is my local script in the tool
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local gotDetected = false
script.Parent.Activated:Connect(function()
mouse.Move:Connect(function()
local target = mouse.Target
if target then
if target.Parent.Team.Name == "M" then
gotDetected = true
print("hooray")
else
gotDetected = false
print("re")
end
end
end)
end)
You need to get the player from the character and then get the players team and your script should work fine. It ought to look like this:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local gotDetected = false
script.Parent.Activated:Connect(function()
mouse.Move:Connect(function()
local target = mouse.Target
if target and target.Parent:FindFirstChild('Humanoid') then
local plr = game.Players:GetPlayerFromCharacter(target.Parent)
if plr.Team.Name == "M" then
gotDetected = true
print("hooray")
else
gotDetected = false
print("re")
end
end
end)
end)
move the mouse.Move outside of the Activated function, since everytime you click, it will connect the mouse.Move event. Move it outside of the Activated connect function, and also don’t forget to disconnect the mouse.Move once you do not need it anymore, this is to prevent memory leaks.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local gotDetected = false
mouse.Move:Connect(function()
script.Parent.Activated:Connect(function()
local target = mouse.Target
if target and target.Parent:FindFirstChild('Humanoid') then
local plr = game.Players:GetPlayerFromCharacter(target)
if plr.Team.Name == "M" then
gotDetected = true
print("hooray")
else
gotDetected = false
print("re")
end
end
end)
end)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local gotDetected = false
script.Parent.Activated:Connect(function()
mouse.Move:Connect(function()
local target = mouse.Target
if target then
if target.Parent:FindFirstChild("Humanoid") then
print(target)
local nplayer = game.Players:GetPlayerFromCharacter(target.Parent) -- Gets the player from target
if nplayer.Team.Name == "M" then -- Checks if the player is in a team named "M"
gotDetected = true
print("hooray")
else --If player isn't in the team named "M", will print "re"
gotDetected = false
print("re")
end
end
end
end)
end)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.Activated:Connect(function()
local target = mouse.Target
if target then
if target.Parent:FindFirstChild("Humanoid") then
print(target)
local nplayer = game.Players:GetPlayerFromCharacter(target.Parent) -- Gets the player from target
if nplayer.Team.Name == "M" then -- Checks if the player is in a team named "M"
print("hooray")
else --If player isn't in the team named "M", will print "re"
print("re")
end
end
end
end)
The code above prints “hooray” or “re” only once, when the tool is clicked, I’m sure this is what you’re seeking for.
And a memory leak is a section/s of memory which are never cleaned up by garbage collection.