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

No Errors, Trying to push player back on part touch...?

Asked by 4 years ago

I'm fairly new to scripting so I may ask for extensive help here from this point on, but pls be patient...

ANyways, I tried used the .Touched event to push the player backwards by 5 studs, did I do the cframe wrong? Dunno... here is the code:

local mmf = game.Players.LocalPlayer.Character
script.Parent.Touched(function(hit)
mmf.Torso.CFrame = CFrame.new(mmf.Torso.CFrame.m-Vector3.new(0,0,-5))
end)

To answer some common questions: Yes, it is inside of a part. Yes, it is a local script. What am I doing wrong?

0
Why have it be a local script inside of a part, and have it try to find "LocalPlayer"? ISeeChase2 14 — 4y
0
well I don't know any alternatives to game.Players.LocalPlayer... Gomenasa1 32 — 4y
0
try something like this: script.Parent.Touched(function(hit) local plr = (hit.Parent) plr.Torso.CFrame = CFrame.new(plr.Torso.CFrame.m-Vector3.new(0,0,-5)) end) AnotherPerson_999 28 — 4y
0
or use R6 AnotherPerson_999 28 — 4y
0
Thanks for the help~ Ur script taught me a few things, but sadly didn't work uwu, got some help in the end tho Gomenasa1 32 — 4y

1 answer

Log in to vote
0
Answered by
Psudar 882 Moderation Voter
4 years ago

Hey man, welcome to Scripting Helpers. I've got a few things about your script to point out and explain, so bear with me. If you really wanna learn how to script though, this will definitely help.

First off, you're using a local script to get the player, which is all fine and good, except that local scripts don't run anywhere outside of the client. Crazy right! Basically, the local script only runs inside of places like StarterPlayerScripts, StarterGui, StarterPack, and StarterCharacterScripts. What do these places all have in common? Access to the local client (Our player). The script needs some sort of access to the local client to run, as these scripts only run for the local player.

Next, the touched event. The touched event is just a function that fires when something is touched, and returns whatever touched it into the argument "Hit" that you're using. So, first off, instead of changing the LocalPlayers position, we need to change their Character's position. The Player and the Character are actually not the same thing. We can use Hit.Parent to solve that issue. I'll explain it more in the comments on the script.

--Psudar
--June 3 2019
--Code inside server script, within a part: 

Debounce = false --So the function wont fire like a billion times, as it does with touched events

script.Parent.Touched:Connect(function (Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then --Check for a humanoid
        if not Debounce then--if Debounce ~= true then, if Debounce == false then
            Debounce = true --Set the debounce, then fire the code: 
            Hit.Parent.HumanoidRootPart.CFrame = --HumanoidRootPart will allow the whole character to move, not just the Torso. 
                CFrame.new(Hit.Parent.HumanoidRootPart.Position - Vector3.new(0 ,0 ,-5))
            wait(1)
            Debounce = false --Reset the "Cooldown"/Debounce. 
        end
    end
end)

I'm gonna link some useful references, goodluck scripting :) If you have any questions, just leave a comment.

1
AAA, This helped so much ty :))) Gomenasa1 32 — 4y
Ad

Answer this question