When running on my studio client it runs just fine, but when i try to run it on a server the camera does not return the the player's Humanoid. Please Help me -_-
script.Parent.Touched:connect(function(hit) print("im hit") local person = game.Players:GetPlayerFromCharacter(hit.Parent) person.Backpack.CutsceneScript:Remove() local player =hit.Parent:WaitForChild("Humanoid") wait() local c = game.Workspace.CurrentCamera c.CameraType = "Custom" wait() c.CameraSubject = hit.Parent:FindFirstChild("Humanoid") print(player.Name) end)
You shouldn't use GetPlayerFromCharacter, you should do,
local person = game.Players:FindFirstChild("" .. hit.Parent.Name)
Cool thing about GetPlayerFromCharacter()
is that it can get the Player as well as the Player's Character for example:
script.Parent.Touched:connect(function(Hit) local Player = game.Players:GetPlayerFromCharacter(Hit.Parent) --Now Getting the Player Player.Character--Its as Simple as that. Now you have access to you Player's Character you can change its Health, WalkSpeed and ect! --Here a example of Changing the Player's Health Player.Character.Humanoid.Health = 50 end)
Now lets implement this new feature about GetPlayerFromCharacter()
that you learnt into your script. One of the Problem I can notice is that if a Part like the Baseplate triggers your touch event script the script will error since GetPlayerFromCharacter()
will return nil which will lead to rest of your code not working. To prevent this I suggest adding a if statement to check if it was a Player that triggered the event.
local Debounce = false--Lets add Debounce to prevent your script from spamming. script.Parent.Touched:connect(function(hit) local person = game.Players:GetPlayerFromCharacter(hit.Parent) --We will need to add a if statement to see if that Part that triggered the event is actually a Player so your script doesn't error if it isn't! if person and Debounce ~= true then--If The Object that triggered the function is controlled by a Player and Debounce is not true then. Debounce = true--Prevent the function for running multiple time while this script is running. person.Backpack.CutsceneScript:Destroy()--Lets change remove to destroy since remove is deprecated and we should avoid using it whenever possible. local c = game.Workspace.CurrentCamera c.CameraType = "Custom" wait() c.CameraSubject = person.Character:FindFirstChild("Humanoid") print(person.Name) end Debounce = false--Since the script is finished we should change it back to false so the script could run again. end)