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

This simple code won't change the torso's CFrame, why?

Asked by 6 years ago

So, I'm trying to change a torso's CFrame if a value in the character equals 1. I made a separate DataStore that saves the value, and it works just fine, this CFrame thing is the only problem. I got no errors in Studio and when I checked the dev console there were still no errors.

Here's the code I used: LOCAL SCRIPT

01local player = game.Players.LocalPlayer
02 
03if player.JAILED.Value == 1 then
04    game.ReplicatedStorage.JailedPersonEvent:FireServer()
05end
06 
07player.Character.Humanoid.Died:Connect(function()
08    if player.JAILED.Value == 1 then
09        game.ReplicatedStorage.JailedPersonEvent:FireServer()
10    end
11end)

SERVER SCRIPT

1game.ReplicatedStorage.JailedPersonEvent.OnServerEvent:Connect(function(player)
2    local character = game.Workspace:WaitForChild(player.Character)
3 
4        character.Torso.CFrame = CFrame.new(1717.323, 226.31, -1283.756)
5 
6end)

Any reason why???

1 answer

Log in to vote
1
Answered by
chomboghai 2044 Moderation Voter Community Moderator
6 years ago

On line 2 of your server script, you said:

local character = game.Workspace:WaitForChild(player.Character)

There's no point in saying that because if player.Character didn't load, then you are searching for nil.

Replace it with:

local character = player.Character or player.CharacterAdded:Wait()

Hope this helps :)

0
Line 2 would error if a player named Part joined the game LOL User#19524 175 — 6y
Ad

Answer this question