Script breaks itself after a few uses and is pretty random

Hello im trying to make a “pole swing” mechanic in where you get attached to a part, jump to break the attachment and get a slight jump boost after that
It works most of the time, except when it decides to break itself for some reason… with "Workspace.Bar.Bar.Script:7: attempt to index nil with ‘Character’ " (and in-game which for some reason the jump boost doesnt work)

Server Side Script (is attached to the part)


local part = script.Parent
local debounce = false

local function weldPlayer(player)

    local character = player.Character
	local rootPart = character and character:FindFirstChild("HumanoidRootPart")

	
	if character and rootPart then

		local weld = Instance.new("WeldConstraint")
        weldObject = Instance.new("ObjectValue")
		weld.Part0 = part
		weld.Part1 = rootPart
		weld.Parent = part
		weldObject.Parent = character.HumanoidRootPart
		weldObject.Name = "WeldIsTrue"
		
		character.Humanoid.PlatformStand = false
		character.HumanoidRootPart.CanCollide = false
		print("is this working?")
		local sound = Instance.new("Sound", character)
		sound.SoundId = "rbxassetid://12222140"
		sound:Play()
	end
end

part.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
      local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		weldPlayer(player)
		game.ReplicatedStorage.Test.OnServerEvent:Connect(function()
			if part:FindFirstChild("WeldConstraint") then
				print("fired the RemoteEvent")
				print("success")
				part.WeldConstraint:Destroy()
				weldObject:Destroy()
				wait(3)
				debounce = false
			end
		end)
	end
end)

Client Side Script (is inside StarterCharacterScripts)

local UserInputService = game:GetService("UserInputService")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Bar = script.Parent.HumanoidRootPart

local remoteEvent = game.ReplicatedStorage.Test

UserInputService.InputBegan:Connect(function(input, event)
	if input.KeyCode == Enum.KeyCode.Space then
		if script.Parent.HumanoidRootPart:FindFirstChild("WeldIsTrue") then
			remoteEvent:FireServer()
			task.wait(0.1)
			if script.Parent:FindFirstChild('Humanoid') ~= nil then
				print("boing")
				script.Parent.HumanoidRootPart:ApplyImpulse(Vector3.new(0, 1000, 0))
			end
	end
	end
end)

Any idea what causes this to break? And why it when in-game (in testing works fine) the impulse doesnt work? Any help will be appreciated!

imagen
Ive now tried it ingame and it breaks with this:

You need to check whether the part that triggered the touched event is parented to the player, as :GetPlayerFromCharacter() will just return nil if it doesn’t find a player, and then that nil value is passed to your weldPlayer function.

Add this code in between the two lines of code I quoted

if player == nil then return end
1 Like

It doesnt break the whole testing now thankfully, but still breaks itself sadly

Nvm fixed it, put an anti-crash stuff thanks to jamie code, in case if it crashes it restarts itself, also rearraganed the code so after touching calls the function and then does the stuff Thx!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.