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?
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.