Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 8 years ago

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)
0
Is this in a LocalScript? ISellCows 2 — 8y

2 answers

Log in to vote
0
Answered by
xuefei123 214 Moderation Voter
8 years ago

You shouldn't use GetPlayerFromCharacter, you should do,

local person = game.Players:FindFirstChild("" .. hit.Parent.Name)
Ad
Log in to vote
0
Answered by 8 years ago

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)
0
this script works but i still face the same issue. IT does not work inside a server only in studio koolkid8099 705 — 8y
0
also u put Destory.... a very minor mistake koolkid8099 705 — 8y
0
@koolkid8099 Thanks for pointing that out! :) Just curious, do you see any errors while you are testing the script in the server? UserOnly20Characters 890 — 8y
0
I do not see any errors at all koolkid8099 705 — 8y
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 koolkid8099 705 — 8y

Answer this question