Hello-
I've created a tool that basically just changes a players team if they are clicked on. The problem is that this tool only works part of the time. The tool usually works the very first time the character spawns but after that, nothing happens and no errors appear.
Here's the basic script:
MouseHold = false local Tool = script.Parent local User Tool.Equipped:connect(function(mouse) mouse.Icon = "http://www.roblox.com/asset/?id=57571495" User = Tool.Parent if User.Humanoid.Health <= 0 then script.Parent:Remove() end mouse.Button1Down:connect(function() local Ray = Ray.new(Tool.Handle.CFrame.p,(mouse.Hit.p - Tool.Handle.CFrame.p).unit * 999) local Hit,Position = game.Workspace:FindPartOnRay(Ray,User) if Hit then local Character = Hit.Parent if Hit.Parent:FindFirstChild("GroupRank") then local Player = game.Players:GetPlayerFromCharacter(Hit.Parent) local Mag = (User.Torso.Position - Hit.Parent.Torso.Position).magnitude if Mag < 20 then local PlayersFirstTeam = Player.TeamColor Player.TeamColor = BrickColor.new("Bright orange") print("Moving on") wait(20) Player.TeamColor = PlayersFirstTeam end end end end) end)
As always, help is appreciated, Thanks!
Problem:
You identified the Variable
Tool outside of the Equipped function
. Then you set the User to the parent of that Tool variable
. Which is a problem because, then you basically identified the Character
outside of the function
, because you have used the Variable
Tool to call on the Character
; so, the Variable
Tool is outside the function
. This would call on the Character
that first had the Tool
. So when your Character
respawns this would not call upon the newly generated Character
, instead it would call on the one that has died.
Solution:
I would advise for you to identify the Tool
inside of the function
so, if you identify the Character
inside of the function by the Tool
, it would be the Character
that the Player
is in-control of at the moment. So, your script needs a few adjustments to it. At the end it should look something like this:
MouseHold = false script.Parent.Equipped:connect(function(mouse) local Tool = script.Parent mouse.Icon = "http://www.roblox.com/asset/?id=57571495" User = Tool.Parent if User.Humanoid.Health <= 0 then script.Parent:Remove() end mouse.Button1Down:connect(function() local Ray = Ray.new(Tool.Handle.CFrame.p,(mouse.Hit.p - Tool.Handle.CFrame.p).unit * 999) local Hit,Position = game.Workspace:FindPartOnRay(Ray,User) if Hit then local Character = Hit.Parent if Hit.Parent:FindFirstChild("GroupRank") then local Player = game.Players:GetPlayerFromCharacter(Hit.Parent) local Mag = (User.Torso.Position - Hit.Parent.Torso.Position).magnitude if Mag < 20 then local PlayersFirstTeam = Player.TeamColor Player.TeamColor = BrickColor.new("Bright orange") print("Moving on") wait(20) Player.TeamColor = PlayersFirstTeam end end end end) end)
I hope that above script helps you out. If you have any other problems with the script please, don't be hesitant to post a comment below.
Thank you for putting your precious time into reading my answer.
~~ KingLoneCat