I made a "script" that's supposed to have my character change position when I touch a brick, but it's not working!

Hi! I’m pokeboss992! Today, I started a game called “Blox Run”, inspired by the classic series called “Run” you might see on game sites. As I said in the title, I’m trying to make a script that has a character change position when touching, sort of how the wall-jumping system works in the game I was inspired by.

As I said before, I made a script for changing the characters position when touching a part, but it doesn’t seem to work. Here’s my script:

 script.Parent.Touched:connect(function(hit)
	local player = hit.Parent
	local wall = script.Parent.Orientation.Z
	player.Torso.CFrame = CFrame.Angles(0,0,wall+90)
end)

Any tips on how to make it working?

Thanks!!

Try this. First off, you want to rotate them, so you want to take their current CFrame and then add the CFrame.Angles to it. Then you also want to use math.rad, which converts degrees into what CFrame.Angles can use. I also would add a debounce so that they don’t go spining out of controls. If you have any questions about this feel free to ask.

local db = false
script.Parent.Touched:connect(function(hit)
	local player = hit.Parent
	local wall = script.Parent.Orientation.Z
        if db == false then
            db = true
	    player.Torso.CFrame = player.Torso.CFrame * CFrame.Angles(0,0,math.rad(wall+90))
            wait(0.5)
            db = false
       end
end)
1 Like

Try what @XdJackyboiiXd21 said but also include a check on the player to ensure the thing that is touching the part is the player itself. An easy way to go about doing this is

if hit.Parent:FindFirstChild('Humanoid') ~= nil then
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    -- rest of your code
end
3 Likes

Yes that is important. He doesn’t need the “GetPlayerFromCharacter” part because he isn’t looking for the player. He is looking for the character.

Then after they get the player correctly they can just add .Character

player.Character.Torso.CFrame = player.Character.Torso.CFrame * CFrame.Angles(0,0,math.rad(wall+90))

or yeah, you’re right, you can use hit.Parent still

Although this is all correct, the most important part is that instead of setting the torso’s position, you should use :SetPrimaryPartCFrame() on the Player’s character, so you don’t kill the player.

Ahh, I get it! So the game just didn’t know that I was talking about angles, and it was converting the number into something different. Thanks a bunch!

Yes, it was converting it into radians, which is another way of measuring angles.

1 Like

Also, I do have another question! When I made the script, was this the correct way of telling the game I wanted to be moved on the “Z” axis?

script.Parent.Orientation.Z

Yes that is correct.

script.Parent.Orientation.Z --will return the degree of the orientation on the Z axis. Then you change the Z axis by doing this:

CFrame.Angles(0,0,math.rad(wall+90))
1 Like