Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does this work in studio client but not a test server?

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)

1 comment

0

Is this in a LocalScript?

2 answers

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)

5 comments

0

this script works but i still face the same issue. IT does not work inside a server only in studio

0

also u put Destory.... a very minor mistake

0

@koolkid8099 Thanks for pointing that out! :) Just curious, do you see any errors while you are testing the script in the server?

0

I do not see any errors at all

0

i added in print("dont") after changing camerasubject to humanoid to see if that part of the script ran and it returned the print in output so im so lost on whats going on

This discussion is preserved as it was. New answers, comments, and votes are closed.