Script not working

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)
1 Like

You are not getting the player at all in this script. Use GetPlayerFromCharacter

3 Likes

This is a local script (30 chars)

3 Likes

It does since you cannot do .Team for a players character

2 Likes

but for the target, you gotta do what topdog said,
get the player from character using game.Players:GetPlayerFromCharacter(character)

Where do I put that???

1 Like

local PlayerTarget= game.Players:GetPlayerFromCharacter(Target.Parent)
if PlayerTarget then
– do stuff to player target
end

1 Like

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)
4 Likes

No errors but its not printing still (Note: I put your code in a local script)

Also @KinqAndi

1 Like

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.

1 Like

I want it to run this every time someone equips the tool though…

What are memory leaks?

1 Like

I tried this, no errors and no success

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)

Can anyone please help me???

1 Like

I don’ want to change teams, I want it so like if a player is on a certain team and you use that tool and click on them something happens

Yeah, I read it incorrectly. You should trying printing the target to see if it’s a player or something.

1 Like

It doesn’t print anything even when the mouse is on player

You should try firing a remote event when the mouse is clicked on a target.

e.g script.Parent.MouseEvent:FireServer(Mouse.Target)

Edit: Maybe you should consider using the teams color instead of name.

This should work, although I haven’t tested it.

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)

Ive gone ahead and tested the code you have provided, I can confirm it works.

1 Like
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.