I’ve tried remote events, but its starting as a script not a local script.
I’m not the greatest scripter ever, I’m more of a builder, and just want to know what its not exactly working. I have the ClickDetector in the part, I have it set so when I click it, it runs code, but for some reason it isnt working. The script its in is a normal Script. Thank you!
The reason of the error is because Character doesn’t exist in the player yet. You’ll have to wait for it to exist. Thankfully I have a better implementation to this where you won’t need to wait for any character. The .MouseClick event takes in a player parameter, and since you have this stored in a LocalScript (not sure if this is intentional or not) the .MouseClick can only ever work for the local player anyway, however if it’s not intended to be in a LocalScript, this script will work just as well in a normal script (server script) too.
local Part = script.Parent
local PartClickDetector = Part:WaitForChild("ClickDetector", 2)
-- 'player' in this case is the Player object of the user that clicked the part.
PartClickDetector.MouseClick:Connect(function(player)
--[[ There wasn't a need in the above script to get the Humanoid's
parent, since that's just the Character anyway, so we'll just get
the character here. :) ]]
local character = player.Character
--[[ And finally, we can set the characters torso colour.
The character rig could be both R6 and R15 so you'll want to account
for this too. We can do this by using :FindFirstChild() which attempts
to return an object, even if it's nil (doesn't exist). So we can check
if the object doesn't exist, if not, it'll most definitely be the other
rig type. ]]
local Torso = character:FindFirstChildChild("Torso")
local UpperTorso = character:FindFirstChildChild("UpperTorso")
local LowerTorso = character:FindFirstChildChild("LowerTorso")
if Torso then
Torso.BrickColor = BrickColor.new("Maroon")
elseif UpperTorso and LowerTorso then
UpperTorso.BrickColor = BrickColor.new("Maroon")
LowerTorso.BrickColor = BrickColor.new("Maroon")
end
end)
This is completely untested, hopefully it works first time.
EDIT: When I said player ‘parameter’, it’s not a player that YOU pass in, it’s a parameter which gets assigned by the event itself (MouseClick).
I just tried that out, my error seems to be with the Character, it says (attempt to index nil with ‘Character’ ) even though its the same way I’ve always gotten the character defined.
Oh, unfortunately I misread the title as I thought THAT color was your issue, just make sure you make it script and not local script and put the player on the first argument, then you’re all set
Oh my gosh! I was not expecting a response like this! I tested it out, and only tweaked 2 things! It works! I see where I went wrong, and I’m going to study it all more so I can hopefully learn from my mistakes! I’ve only started scripting a couple weeks ago! Thank you so much : D
Not a problem, I’m glad I could help . I know which two things you tweaked, I forgot to remove spaces that got in there for some reason between the upper and lower torso variables and the ‘.BrickColor’ property.
When you’re calling events, the parameters that get passed in the connected function are filled in by the event itself, so you don’t need to pass in a player object. For example:
local Player = game:GetService("Players").LocalPlayer
ClickDetector.MouseClick:Connect(function(PlayerThatsPassedInByMouseClick)
end)
Player and PlayerThatsPassedInByMouseClick are two completely different objects but they’re still player objects. Therefore, it cancels out the need for the Player variable.